最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Enemy walking through everything, using rb.MovePosition - Stack Overflow

programmeradmin5浏览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;
            }
        }
    }

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
Add a comment  | 

1 Answer 1

Reset to default 0

To make Navmesh agents avoid obstacles, there's a component called NavmeshObstacle that you add to the gameobject you want to avoid.

发布评论

评论列表(0)

  1. 暂无评论