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

'LookAt' script causing forward movement

Discussion in 'Scripting' started by 2close4missiles, Mar 29, 2015.

  1. 2close4missiles

    2close4missiles

    Joined:
    Feb 13, 2015
    Posts:
    13
    EDIT: I got it figured out :p


    So I was attempting to create a script that makes an enemy look at the player. This much works, however the enemy (which is just a cube right now) slowly inches toward the player constantly:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyScript : MonoBehaviour
    5. {
    6.     public Transform player;
    7.     Quaternion targetLook;
    8.     public float smoothLook = .5f;
    9.  
    10.  
    11.     void Start ()
    12.     {
    13.         player = GameObject.FindWithTag("Player").transform;
    14.     }
    15.  
    16.     void Update ()
    17.     {
    18.         targetLook = Quaternion.LookRotation (new Vector3 (player.position.x - transform.position.x,
    19.                                                            player.position.y - transform.position.y,
    20.                                                            player.position.z - transform.position.z));
    21.  
    22.         transform.rotation = Quaternion.Lerp(transform.rotation, targetLook, smoothLook * Time.deltaTime);
    23.     }
    24. }
    25.  
    Can anyone explain this? Please and thanks!
     
    Last edited: Mar 29, 2015
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Why not use transform.LookAt?

    Code (CSharp):
    1. public Transform player;
    2.  
    3. void Update()
    4. {
    5. transform.LookAt(player);
    6. }
    I guess what you're doing wrong, is with the logic. You try to rotate something from the center point of the object, then after that you save the rotation axis and then rotate once again from the center point of the object. This is just what I think, I'm sadly not that experienced with Vectors and stuff.
     
  3. 2close4missiles

    2close4missiles

    Joined:
    Feb 13, 2015
    Posts:
    13
    That works but using Quaternion.Lerp allows for smoothing as well as a delay when updating.
     
    Sykoo likes this.
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're using Lerp incorrectly. The value for T should never be a delta time step and the start position should not be changing every frame. Here's a great article: https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/

    You can fix your issue without using Lerp however, just use RotateTowards instead:
    Code (csharp):
    1.  
    2.     Vector3 relativePosition = player.position - transform.position;
    3.     Quaternion targetRotation = Quaternion.LookRotation(relativePosition);
    4.  
    5.      transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 180.0f * Time.deltaTime);
    6.  
    Where 180.0f represents 180 degrees a second. Adjust accordingly.

    Best of luck with your project.
     
    2close4missiles likes this.
  5. 2close4missiles

    2close4missiles

    Joined:
    Feb 13, 2015
    Posts:
    13
    Yeah, that's actually what I ended up doing. Thanks for the help!