2D (look at) object disappears
I have a moving object that I want to look at the target it's moving towards, here is my code:
void Update()
{
transform.LookAt( target.transform.position);
}
The problem is the object does look at the target while moving, but since the game is in 2D view, the object kinda turns sideways, what is happening is that it turns and faces the object it's looking at.
Answer by Glurth · Mar 07, 2016 at 05:34 PM
Not sure, but it sound like you need to specify the LookAt function with the "up" direction. If you are using a 2D space this will be the coordinate you are NOT using. So if you are using x, and z for the 2D space, specify the lookAt "up" parameter using new Vector3(0,1,0)
(if using x and y for your 2D space, then specify (0,0,1) for "up")
Note: by default, if you DON'T provide the "up" direction, it will use Vector3.up=(0,1,0) [so, if you ARE using X and Z in your 2D space, this answer is NOT the solution.]
Edit:
Some more detail about using the LookAt Function: This is a function that can take one OR two parameters. If you only give it one; it will automatically pass a "default" value (for LookAt, the default value is Vector3.up). http://docs.unity3d.com/ScriptReference/Transform.LookAt.html
Since the lookat function deals with 3D vectors, you may need to specify the second parameter yourself.
When you "look at" something, in the real world, and tilt your head, you are changing the second parameter for the LookAt Function.
If you use the wrong value for this "head tilt", in your 2D world, it might turn your object "into" the screen (and from the edge a 2D object is not visible since it has no thickness.)
I don't really understand, could you please simplify things further?!
Your answer
Follow this Question
Related Questions
I can't seem to rotate my character depending on what side he is running to (2D sidescroller) 0 Answers
How to make a 2D rigidbody vertically when input is via the gyroscope 0 Answers
Beginner Question about moving 0 Answers
How to Transform.Rotate Around with the rotation of the controller right stick 0 Answers