Jump to content
  • Advertisement

Recommended Posts

I am having a problem with making duplicates of my AI. When one is within range, it performs normally. If two are in range, then they both freeze up. The AI are direct duplicates of each other. I don't know what causes this. Code below:

Any help is seriously appreciated. Thank you.

Code Below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class NavMeshAI : MonoBehaviour {
public Transform player;
public Transform head;
public Animator anim;
public bool pursuing = false;
public NavMeshAgent navComponent;
public Transform enemy;

// Use this for initialization
void Start () {
    anim = GetComponent<Animator> ();
    navComponent = this.gameObject.GetComponent<NavMeshAgent>();
}

// Update is called once per frame
void Update () {
    Vector3 direction = player.position - this.transform.position;
    direction.y = 0;

    float angle = Vector3.Angle (direction, head.up);

    if (Vector3.Distance (player.position, this.transform.position) < 10 && (angle < 360 || pursuing)) {

        pursuing = true;
        this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);

        anim.SetBool ("isIdle", false);
        if (direction.magnitude > 2) {
            this.transform.Translate (0, 0, 0.05f);
            anim.SetBool ("isWalking", true);
            anim.SetBool ("isAttacking", false);
            if (pursuing == true) {
                navComponent.SetDestination (player.position);
            }
            } else {
            anim.SetBool ("isAttacking", true);
            anim.SetBool ("isWalking", false);
        }
    } else {
        anim.SetBool ("isIdle", true);
        anim.SetBool ("isWalking", false);
        anim.SetBool ("isAttacking", false);
        pursuing = false;
        navComponent.SetDestination (enemy.position);
    }
}

 

Share this post


Link to post
Share on other sites
Advertisement

It's not clear what all your public variables are, so it's hard to be sure. If 'enemy' was the same character as the one doing the AI, its easy to see how the last line of navigation would actually do nothing, for example.

Use Debug.Log lines to see which code branches are being executed, and which values the code has at that time.

Share this post


Link to post
Share on other sites

The first thing that looks suspicious to me is the Vector3.Angle.  According to the documentation:

"The angle returned is the unsigned acute angle between the two vectors. This means the smaller of the two possible angles between the two vectors is used. The result is never greater than 180 degrees."

It looks like you're measuring the angle between the direction between you and the player vs. the "up" direction of your head.  I suppose if your head transform's "up" is supposed to be facing towards the player this might be useful but it's a bit weird.

But then you use it with "angle < 360".  The Vector3.Angle documentation says that the angle returned cannot be greater than 180 degrees.  So this is always true, and that condition won't contribute to the code working or not.  So we can eliminate that from the things that might cause your bug.

The rest of the code doesn't look like it has anything that might influence a different instance.

 

You say you "duplicate" your AI?  Can you show us the code that does that?  I think the problem is related to that, or at the very least not in the code you posted above.

 

Also, what is 'enemy' assigned to?  Is it the player?  Is it the AI?  Is it a third object?

Edited by Nypyren

Share this post


Link to post
Share on other sites

I commented out your Animation code and it worked fine.

So it is either something wrong with your animation or it is the way you duplicate. I just right clicked and selected "Duplicate" this also fills the script with the correct targets to each AI.

I just used a quick file I had.

AI.jpg.96a395f8a280de57c3d5b0861943eb8c.jpg

The code:

Spoiler



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class NavMeshAI : MonoBehaviour {
	public Transform player;
	public Transform head;
	//public Animator anim;
	public bool pursuing = false;
	public NavMeshAgent navComponent;
	public Transform enemy;

	// Use this for initialization
	void Start () {
		//anim = GetComponent<Animator> ();
		navComponent = this.gameObject.GetComponent<NavMeshAgent>();
	}

	// Update is called once per frame
	void Update () {
		Vector3 direction = player.position - this.transform.position;
		direction.y = 0;

		float angle = Vector3.Angle (direction, head.up);

		if (Vector3.Distance (player.position, this.transform.position) < 10 && (angle < 360 || pursuing)) {

			pursuing = true;
			this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);

			//anim.SetBool ("isIdle", false);
			if (direction.magnitude > 2) {
				this.transform.Translate (0, 0, 0.05f);
				//anim.SetBool ("isWalking", true);
				//anim.SetBool ("isAttacking", false);
				if (pursuing == true) {
					navComponent.SetDestination (player.position);
				}
			}
		} else {
			//anim.SetBool ("isIdle", true);
			//anim.SetBool ("isWalking", false);
			//anim.SetBool ("isAttacking", false);
			pursuing = false;
			navComponent.SetDestination (enemy.position);
		}
	}
}

 


 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!