Search Unity

LookAt moves instead of just looking

Discussion in 'Scripting' started by asdx, Oct 17, 2013.

  1. asdx

    asdx

    Joined:
    Jun 3, 2013
    Posts:
    18
    Hello. Since the answers page is 99% of the time not working I'm posting my question here, nope someone can help.

    I have a simple LookAt script attached to a cube. Instead of just looking at the target the cube MOVES.. wt...:confused:
    Here is the code:
    Code (csharp):
    1.     void Update()
    2.     {
    3.         if(Physics.Raycast(transform.position, transform.forward, out hit, 8))
    4.         if (hit.collider.tag == "Player")
    5.         {
    6.             print("found a player");
    7.             transform.LookAt(hit.transform);
    8.         }
    9.     }
    That is all the code.. What am I missing here. I think in the previous version of Unity this didn't happen.
    Anyway, eventually I will replace the cube with a character with animation.. so if you'd rather suggest something for that then I'll gladly accept that advice too.
     
  2. asdx

    asdx

    Joined:
    Jun 3, 2013
    Posts:
    18
    Nevermind... I found that the Y was changing so I used this instead:
    Code (csharp):
    1.  transform.LookAt(new Vector3(hit.transform.localPosition.x, transform.position.y, hit.transform.localPosition.z));
    So it keeps it's own y but follows the x and z of the player