简体   繁体   中英

AI Not Working?

hello i'm trying to create an ai for my characters but it doesnt work i want it to shoot my character on sight but it's just wandering in the scene and not shooting not even my debug.log works

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);
}

The distance is simply never less than 30. In your else:

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

do Debug.Log(Vector3.Distance(transform.position, Player.position)); so you can see what values you're getting:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM