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

Enemy face direction of travel

Discussion in 'Scripting' started by How90, Aug 28, 2014.

  1. How90

    How90

    Joined:
    Aug 28, 2014
    Posts:
    8
    I want the object or zombie in this case to face the direction of is movement, I have a script that cause random movements for the zombie but it never faces the direction of travel

    Code (JavaScript):
    1.     var speed : Vector3;
    2.     var timing :float;
    3.     function Start() {
    4.        speed = Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359));
    5.        timing = 0;
    6.     }
    7.     function FixedUpdate() {
    8.        timing+=Time.deltaTime;
    9.        rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
    10.         if(timing > 3){
    11.            ChangeDirection();
    12.            timing = 0;
    13.         }
    14.     }
    15.     function ChangeDirection () {
    16.         speed = new Vector3(Random.Range(-359, 359),0,Random.Range(-359, 359),0);  }
     
    Last edited: Aug 28, 2014
  2. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
  3. How90

    How90

    Joined:
    Aug 28, 2014
    Posts:
    8
    Sorry new here, fixed :)
     
  4. Nemanja_DarkOne

    Nemanja_DarkOne

    Joined:
    Dec 11, 2013
    Posts:
    8
    You have to rotate it to face the direction in which he is moving. So every time you change the direction he is moving you rotate him so he will face it
     
  5. How90

    How90

    Joined:
    Aug 28, 2014
    Posts:
    8
    How do I calculate where he is facing?
    transform.Lookat(?);

    I don't know what he has to look at

    Thanks for all the help
     
  6. How90

    How90

    Joined:
    Aug 28, 2014
    Posts:
    8
    Anyone know how to calculate the direction he will be going so I can face him that way?

    Thank you
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. Vector3 newPos = rigidBody.position + speed * Time.deltaTime;
    3. transform.LookAt(newPos);
    4. rigidBody.MovePosition(newPos);
    5.  
     
    How90 likes this.
  8. How90

    How90

    Joined:
    Aug 28, 2014
    Posts:
    8
    Noob here don't really know how to implement that in the code in JavaScript
     
  9. How90

    How90

    Joined:
    Aug 28, 2014
    Posts:
    8
    Sorry guys I'm still so lost on this, only been on unity two weeks
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    90% of what I wrote is from your own code. Replacing Vector3 newPos with var newPos should work fine.
     
  11. How90

    How90

    Joined:
    Aug 28, 2014
    Posts:
    8
    So I just add all that in? In the fixed update ? And the change direction?
     
  12. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Yes. You're going to want to evaluate that every time. For a Javascript variable, use var instead of Vector3. Other than that, the code should work.
    And yes, put it in fixed update.
     
    How90 likes this.
  13. How90

    How90

    Joined:
    Aug 28, 2014
    Posts:
    8
    Code (CSharp):
    1. #pragma strict
    2.  
    3. var speed : Vector3;
    4. var timing :float;
    5.     function Start()
    6.     {
    7.        speed = Vector3(Random.Range(-5, 5),0,Random.Range(-5, 5));
    8.        timing = 0;
    9.     }
    10.     function FixedUpdate()
    11.     {
    12.         timing+=Time.deltaTime;
    13.         rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
    14.         var newPos = rigidbody.position + speed * Time.deltaTime;
    15.         transform.LookAt(newPos);
    16.         rigidbody.MovePosition(newPos);
    17.         if(timing > 3)
    18.         {
    19.            ChangeDirection();
    20.            timing = 0;
    21.         }
    22.     }
    23.     function ChangeDirection ()
    24.         {
    25.         speed = new Vector3(Random.Range(-5, 5),0,Random.Range(-5,5 ));
    26.         }
    thank you so much guys exactly what i needed, if anyone wants this, it works :)

    Just change the -359,359 in all places to alter speed
     
    Last edited: Aug 31, 2014