I am trying to get this model to recognise collisions and avoid objects, such as the player. I realised Navmesh agents don't detect physics, so I am now using Rigidbody.MovePosition to move it around some waypoints, but it walks through everything in its path. It has a capsule collider on it as do the items I've tested it on, plus they all have a rigidbody. I'm no expert as you can guess, any help would be appreciated. I want it to be able to knock into things
public List<GameObject> waypoints;
public Rigidbody rb;
int index = 0;
public float speed = 2f;
Animator animator;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
void FixedUpdate()
{
Vector3 destination = waypoints[index].transform.position;
Vector3 direction = (destination - transform.position);
float distance = Vector3.Distance(rb.transform.position, destination);
// Debug.Log("distance = " + distance);
Debug.Log("destination = " + destination);
rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(direction);
if (distance <= 1)
{
index++;
Debug.Log("count = " + index);
if (index >= waypoints.Count)
{
index = 0;
}
}
}
I am trying to get this model to recognise collisions and avoid objects, such as the player. I realised Navmesh agents don't detect physics, so I am now using Rigidbody.MovePosition to move it around some waypoints, but it walks through everything in its path. It has a capsule collider on it as do the items I've tested it on, plus they all have a rigidbody. I'm no expert as you can guess, any help would be appreciated. I want it to be able to knock into things
public List<GameObject> waypoints;
public Rigidbody rb;
int index = 0;
public float speed = 2f;
Animator animator;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
void FixedUpdate()
{
Vector3 destination = waypoints[index].transform.position;
Vector3 direction = (destination - transform.position);
float distance = Vector3.Distance(rb.transform.position, destination);
// Debug.Log("distance = " + distance);
Debug.Log("destination = " + destination);
rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(direction);
if (distance <= 1)
{
index++;
Debug.Log("count = " + index);
if (index >= waypoints.Count)
{
index = 0;
}
}
}
Share
Improve this question
asked Mar 22 at 9:59
Annette GAnnette G
112 bronze badges
2
- You need to ensure the player can collide with other items (can OnCollisionEnter get invoked?). Usually static objects do not need a rigidbody unless you want to push them. – shingo Commented Mar 22 at 10:55
- Thanks, in fact I had the collider on the enemy set to trigger, no idea why. Thanks for replying, now I have a new problem enemy flying off into the air, just working on that – Annette G Commented Mar 22 at 12:20
1 Answer
Reset to default 0To make Navmesh agents avoid obstacles, there's a component called NavmeshObstacle that you add to the gameobject you want to avoid.