首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在对象完成移动后一直得到空异常?

为什么在对象完成移动后一直得到空异常?
EN

Stack Overflow用户
提问于 2018-04-13 22:29:46
回答 1查看 67关注 0票数 0
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoidFlocking : MonoBehaviour
{
    public Vector3 velocity;

    private float cohesionRadius = 10;
    private float separationDistance = 5;
    private Collider[] boids;
    private Vector3 cohesion;
    private Vector3 separation;
    private int separationCount;
    private Vector3 alignment;
    private float maxSpeed = 15;

    private void Start()
    {
        InvokeRepeating("CalculateVelocity", 0, 1f);
    }

    void CalculateVelocity()
    {
        velocity = Vector3.zero;
        cohesion = Vector3.zero;
        separation = Vector3.zero;
        separationCount = 0;
        alignment = Vector3.zero;

        boids = Physics.OverlapSphere(transform.position, cohesionRadius);
        foreach (var boid in boids)
        {
            cohesion += boid.transform.position;
            if (boid == null)
                print("It's null !!!!!");

            alignment += boid.GetComponent<BoidFlocking>().velocity;

            if (boid != GetComponent<Collider>() && (transform.position - boid.transform.position).magnitude < separationDistance)
            {
                separation += (transform.position - boid.transform.position) / (transform.position - boid.transform.position).magnitude;
                separationCount++;
            }
        }

        cohesion = cohesion / boids.Length;
        cohesion = cohesion - transform.position;
        cohesion = Vector3.ClampMagnitude(cohesion, maxSpeed);
        if (separationCount > 0)
        {
            separation = separation / separationCount;
            separation = Vector3.ClampMagnitude(separation, maxSpeed);
        }
        alignment = alignment / boids.Length;
        alignment = Vector3.ClampMagnitude(alignment, maxSpeed);

        velocity += cohesion + separation * 10 + alignment * 1.5f;
        velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
    }

    void Update()
    {
        if (transform.position.magnitude > 25)
        {
            velocity += -transform.position.normalized;
        }

        transform.position += velocity * Time.deltaTime;

        Debug.DrawRay(transform.position, separation, Color.green);
        Debug.DrawRay(transform.position, cohesion, Color.magenta);
        Debug.DrawRay(transform.position, alignment, Color.blue);
    }
}

我现在为检查添加了这个部分,但是boid永远都不是空的:

代码语言:javascript
复制
if (boid == null)
                    print("It's null !!!!!");

空异常位于行上:

代码语言:javascript
复制
alignment += boid.GetComponent<BoidFlocking>().velocity;

我有5个球体,脚本相同,速度设置为X=1Y=1Z=1

我试着在这里学习这个教程:

Unity3D中的Boid与鸟类

但我不明白为什么要得到空异常。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-13 23:26:39

如果空值并不重要,那么只需用以下方式编写:

代码语言:javascript
复制
alignment += boid?.GetComponent<BoidFlocking>()?.velocity ?? Vector3.zero;

这一行表示,如果boidGetComponent<BoidFlocking>()为null,则使用Vector3.zero作为默认值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49825941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档