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 it disappears from the screen, but it’s there at the same time
lookat is for 3d, and it is using z-axis (you can rotate your sprite at 90 on y-axis(not sure), so you cannot see them in editor).
try something here
Is there a Transform.LookAt() equivalent that will work for 2D? Without turning the gameobject on it’s side that is… Or alternatively does anyone have a workaround using Eulers or something ? Thanks in advance 🙂 Edit: Solution Quaternion rotation...
Reading time: 2 mins 🕑
Likes: 2 ❤
1 Like
system
March 8, 2016, 8:20am
3
Agree with @vakabaka .
I use this code to make 2D objects face a point:
public static void LookAt2D(this Transform transform, Vector2 target)
{
Vector2 current = transform.position;
var direction = target - current;
var angle = Mathf.Atan2(direction.y, direction.x)*Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
4 Likes
Thank you, that works perfect. On other note, I think there are some 3D functions in unity that needs 2D versions.
Agree with @vakabaka .
I use this code to make 2D objects face a point:
public static void LookAt2D(this Transform transform, Vector2 target)
{
Vector2 current = transform.position;
var direction = target - current;
var angle = Mathf.Atan2(direction.y, direction.x)*Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
How do I add speed to that?
do you mean rotate speed ? Calculate angle and rotate gameobject with needed speed. Something like this
public float rotateSpeed;
void Update()
{
Vector2 dir = enemy.transform.position - transform.position;
float angle = Vector2.Angle(transform.right, dir);
if (angle > 0.5f)
{
float rDir = Mathf.Sign(Vector2.SignedAngle(transform.right, dir));
transform.Rotate(Vector3.forward * Time.deltaTime * rDir * rotateSpeed);
}
}
Agree with @vakabaka .
I use this code to make 2D objects face a point:
public static void LookAt2D(this Transform transform, Vector2 target)
{
Vector2 current = transform.position;
var direction = target - current;
var angle = Mathf.Atan2(direction.y, direction.x)*Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
last time I am using this:
Vector3 dir = target.transform.position - transform.position;
gameObject.transform.right = dir;
2 Likes
Agree with @vakabaka .
I use this code to make 2D objects face a point:
public static void LookAt2D(this Transform transform, Vector2 target)
{
Vector2 current = transform.position;
var direction = target - current;
var angle = Mathf.Atan2(direction.y, direction.x)*Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
Just curious how does this work?
Agree with @vakabaka .
I use this code to make 2D objects face a point:
public static void LookAt2D(this Transform transform, Vector2 target)
{
Vector2 current = transform.position;
var direction = target - current;
var angle = Mathf.Atan2(direction.y, direction.x)*Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
How do you use this? My Class name highlights in red when i add it to my script
I did it with the following code, note that I didn’t need the character to be rotated towards the target so a simple sprite flip was enough for me based on where the transform was compared to the target transform.
float lookAtDirection = target.position.x - transform.position.x;
if(lookAtDirection < 0 )
{
// if sprite is looking at right flip else do nothing
this.GetComponent<SpriteRenderer>().flipX = true;
}
else if(lookAtDirection > 0 )
{
// if sprite is looking at right do nothing else flip
this.GetComponent<SpriteRenderer>().flipX = false;
}