admin管理员组文章数量:1405361
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.
本文标签: cEnemy walking through everythingusing rbMovePositionStack Overflow
版权声明:本文标题:c# - Enemy walking through everything, using rb.MovePosition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744319752a2600438.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论