Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gameobject to "run" away from another on collide trigger

Discussion in 'Scripting' started by frasderp, Jun 4, 2020.

  1. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    So... I have a gameobject with a collider trigger setup. I want it to detect while another specific gameobject (tag "player") is within the collider, to turn and run away.

    I have the movement working, however the rotation is not working. It appears to "flicker", I believe because it is continuously recalculating the angle of transform.forward back and forth.

    Note, that I want the gameobject to continue running away, until the player is out of the sphere collider (like it is "escapaing").

    Here is my code currently:

    Code (CSharp):
    1.     void OnTriggerStay(Collider CollisionObject)
    2.     {
    3.  
    4.         if (CollisionObject.tag == "Player")
    5.         {
    6.  
    7.         // Rotate gameobject to face "away" from the player who entered the collider
    8.             Vector3 targetDirection = CollisionObject.transform.position - transform.position;
    9.             Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, -180, 10);
    10.             Debug.DrawRay(transform.position, newDirection, Color.red);
    11.             transform.rotation = Quaternion.LookRotation(newDirection);
    12.  
    13.         // (This section works as expected) script to move gameobject away from player.
    14.             transform.position = Vector3.MoveTowards(transform.position, CollisionObject.transform.position, -1 * walkSpeed * Time.deltaTime);
    15.         }
    16.     }
    Appreciate any advice on what I am doing wrong...
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,891
    Which object is the player and which is the running object?

    One thing I see that is weird is your 3rd argument to Vector3.RotateTowards:
    -180


    Vector3.RotateTowards takes an angle in radians. 180 radians is almost 29 full rotations! You probably want to use
    -Mathf.PI
    as your argument, which is equal to 180 degrees of rotation. Why did Unity use radians for Vector3.RotateTowards? Beats me!
     
    Last edited: Jun 4, 2020
  3. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    Sorry, the collision object with tag player, is player. The script is attached to the running object.

    Thanks for the comment regarding radians, I will have a play with that also.

    A question regarding RotateTowards though, how can I tell it to rotatetowards away from the player? I guess I need to calculate the direction between the two objects to determine the angle?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,891
    SO if CollisionObject is the player and transform is the "runner", then accoding to this:
    Code (CSharp):
    1. Vector3 targetDirection = CollisionObject.transform.position - transform.position;
    "targetDirection" will be a Vector pointing from the runner towards the player.

    But then you're using a negative value in RotateTowards so this should cancel out to rotating away from the player. However it would be simpler if you got rid of the double negative:
    Code (CSharp):
    1.     void OnTriggerStay(Collider CollisionObject)
    2.     {
    3.         if (CollisionObject.tag == "Player")
    4.         {
    5.             // Rotate gameobject to face "away" from the player who entered the collider
    6.             Vector3 targetDirection = transform.position - CollisionObject.transform.position;
    7.             Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, Mathf.PI, 10);
    8.             Debug.DrawRay(transform.position, newDirection, Color.red);
    9.             transform.rotation = Quaternion.LookRotation(newDirection);
    10.             // (This section works as expected) script to move gameobject away from player.
    11.             transform.position = Vector3.MoveTowards(transform.position, CollisionObject.transform.position, -1 * walkSpeed * Time.deltaTime);
    12.         }
    13.     }
    I'm not sure that will fix your problem though... The cheaty way would be this:
    Code (CSharp):
    1.     void OnTriggerStay(Collider CollisionObject)
    2.     {
    3.         if (CollisionObject.tag == "Player")
    4.         {
    5.             // Rotate gameobject to face "away" from the player who entered the collider
    6.             Vector3 targetDirection = transform.position - CollisionObject.transform.position;
    7.             Debug.DrawRay(transform.position, targetDirection, Color.red);
    8.             transform.rotation = Quaternion.LookRotation(targetDirection);
    9.             // (This section works as expected) script to move gameobject away from player.
    10.             transform.position = Vector3.MoveTowards(transform.position, CollisionObject.transform.position, -1 * walkSpeed * Time.deltaTime);
    11.         }
    12.     }