Search Unity

Making my enemy wander with MovePosition but it is not colliding

Discussion in 'Physics' started by hissora, Sep 7, 2019.

  1. hissora

    hissora

    Joined:
    Jun 11, 2019
    Posts:
    1
    Hello! I'm currently working on an enemy script for a project and I want the enemy to wander in a random direction until the player enters it's look radius I have created. I know that if I want the enemy to collide with static colliders I need to be using Rigidbody.MovePosition however the enemy is still passing through the environment walls with colliders that I set up. Is there a way to rewrite this so it works how I'm intending? Or is there something else I am missing here?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class EnemyController : MonoBehaviour {
    7.  
    8.     public float lookRadius = 10f;
    9.     public float wanderTime;
    10.  
    11.     public Rigidbody rb;
    12.  
    13.     Transform target;
    14.     NavMeshAgent agent;
    15.  
    16.     void Start()
    17.     {
    18.         target = PlayerManager.instance.player.transform;
    19.         agent = GetComponent<NavMeshAgent>();
    20.         rb = this.GetComponent<Rigidbody>();
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.  
    26.     }
    27.  
    28.     void FixedUpdate()
    29.     {
    30.         float distance = Vector3.Distance(target.position, transform.position);
    31.         if (wanderTime > 0) {
    32.  
    33.             rb.MovePosition(transform.position + transform.forward * agent.speed * Time.deltaTime);
    34.             wanderTime -= Time.deltaTime;
    35.          
    36.  
    37.             if (distance <= lookRadius)
    38.             {
    39.                 agent.SetDestination(target.position);
    40.  
    41.                 if (distance <= agent.stoppingDistance)
    42.                 {
    43.                     // Attack the Target
    44.                     // Face the Target
    45.                 }
    46.             }
    47.         }
    48.         else
    49.         {
    50.             wanderTime = Random.Range(5.0f, 15.0f);
    51.             Wander();
    52.         }
    53.     }
    54.  
    55.     void Wander ()
    56.     {
    57.         transform.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
    58.     }
    59. }
    60.  
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Yes... this interrogates about Rigidbody.MovePosition... according to my humble experience it makes objects pass through each others, even they have physics components... it's maybe normal because basically its, somehow, a Teleportation like when using transform.position. I made some tests (i never used Rigidbody.MovePosition for colliding objects... ) and they confirm what was said above. And yes the object has a Kinematic Rb.

    Note that Unity talk about
    Transition using FixedUpdate not Movement but this is a bit rhetoric...
     
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    That's wrong. MovePosition makes your object move to the target position, no matter what's in between. If you want your Rigidbody to collide with the environment you have to move it with AddForce only.
     
    xorpheous likes this.
  4. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    This is a bit intriguing ... What is sure is that objects moved by MovePosition aren't affected by physics.
    But they can affect physics of objects moved by Force.(vid1)
    They can also affect Rigidbodies that are able to move.(in position or rotation whatever).(vid 2)

    vid1
    the red sphere is moved by MovePosition, the yellow one is moved by AddForce. The cube is
    frozen(Kinematic or By enabling the pos/rot constraints. same result)


    vid2
    the red sphere is moved by MovePosition. The cube has a Rigidbody frozen in position. We can see that the physics is
    coherent even if the sphere pass through the cube collider.
     
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Sort of. If the rigidbody is non-kinematic then physics is being calculated for it, but you're overriding the result by forcing it to move to your target position (otherwise it would move based on the physics). Some times this causes side effects as both you and the physics are "competing" to move the rigidbody.

    If the rigidbody is kinematic then it's exactly as you described: it's not affected by physics, but you move it with MovePosition (always from FixedUpdate, btw) and it affects the other non-kinematic objects. All this is by design.
     
  6. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Right and the behaviour is not realy previsible... side effects in a case, other results (weird some times) in other cases...

    I thought that the use of Kinematic Rb when using MovePosition was a theorem :p