AI不工作?

时间:2017-01-17 09:33:21

标签: unity3d artificial-intelligence

你好,我试图为我的角色创建一个ai,但它不起作用 我想让它在视线中拍摄我的角色,但它只是在场景中徘徊而不是拍摄 甚至我的debug.log都没有工作

public Transform[] Targets;
private int DestPoint = 0;
private NavMeshAgent Agent;
public Transform Player;
public Rigidbody Bullet;
public Transform Instantiator;

void Start()
{
    Agent = GetComponent<NavMeshAgent> ();
    Agent.autoBraking = false;
}

void Update()
{
    if (Vector3.Distance(transform.position, Player.position) < 30f)
    {
        Debug.Log ("Shoot");
        transform.LookAt (Player);
        Shoot ();
    }
    else if (Vector3.Distance(transform.position, Player.position) > 30f)
    {
        GotoNextPoint ();
    }
}

void GotoNextPoint()
{
    Agent.destination = Targets [DestPoint].position;
    DestPoint = (DestPoint + 1) % Targets.Length;
}

void Shoot()
{
    Rigidbody Clone = Instantiate (Bullet, Instantiator.position, Instantiator.rotation) as Rigidbody;
    Clone.AddForce (Vector3.forward);
}

1 个答案:

答案 0 :(得分:1)

距离永远不会小于30.在你的其他地方:

else if (Vector3.Distance(transform.position, Player.position) > 30f)
{
    GotoNextPoint ();
}

Debug.Log(Vector3.Distance(transform.position, Player.position)); 所以你可以看到你得到的价值:

else if (Vector3.Distance(transform.position, Player.position) > 30f)
{
    Debug.Log(Vector3.Distance(transform.position, Player.position));
    GotoNextPoint ();
}