- Home /
Rotating a specific axis towards a object
Hello everyone!
After a few hours of research, i was not able to find a solution to my problem. I would like to make my 2D sprite "look at" another 2D sprite and then go towards it. But i am stuck with the rotating, i tried using LookAt() but then the sprite rotates on a wrong axis and the sprite flies out of the picture.
So basicly i just want it to rotate towards the other sprite but only on the Z-axis.
Did you search "unity rotate one axis"? There are a lot of questions here like yours.
The trick is usually to "flatten" the look point by copying and setting the x,y or z to your own. If you want to use x or y to look (LookAt aims Z,) the child trick can fix it. Very common question/answers about that stuff.
Yes, i searched for that, but i didn't find any solutions. I don't understand what you mean, i want it to "LookAt" another sprite so i can't just set the rotation?
Answer by Frede1012 · Jul 02, 2013 at 02:56 PM
I fixed this myself by switching to Orthello 2D Framework and using a built in feature for sprites called RotateTowards()!
Answer by robertbu · Jul 01, 2013 at 09:42 PM
@Owne Reynolds mentions the typical solution, but I've found it does not work when you want to rotate around the 'Z' axis. It does look in the right place, but it can result in the object flipping. As an alternate solution, you can use Mathf.Atan2() and do your rotations in 2D. The following code assumes the right side of your object is what you want to look at the target. If you are using a plane, consider creating a vertical plane using the CreatePlane editor script.
pragma strict
var target : Transform;
var speed = 0.75;
function Update() {
var v3Dir = target.position - transform.position;
var angle = Mathf.Atan2(v3Dir.y, v3Dir.x) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3(0,0,angle);
if (Input.GetKey(KeyCode.W))
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
I tossed in a Translate towards the target when the 'W' key is pressed.
using UnityEngine;
using System.Collections;
public class $$anonymous$$yLook : $$anonymous$$onoBehaviour {
public Transform target;
public float speed = 0.75f;
void Update() {
Vector3 v3Dir = target.position - transform.position;
var angle = $$anonymous$$athf.Atan2(v3Dir.y, v3Dir.x) * $$anonymous$$athf.Rad2Deg;
transform.eulerAngles = new Vector3(0,0,angle);
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
This does not rotate the sprite so it faces the other sprite?
You need to drag and drop the other sprit onto the 'target' variable in the inspector. This code rotates the sprite it is attached to so that the right side of the sprite faces the other sprite.
Another way to avoid flipping is to set UP in the lookAt. For a z-spin, the object would likely be facing North or South. So transform.LookAt(somePos, -Vector3.foward);
will keep the y axis always pointed South, for example.
Answer by servival · Jun 27, 2015 at 04:01 PM
using UnityEngine;
using System.Collections;
public class AISmoothLookAT : MonoBehaviour {
public Transform target;
public Transform looker;
public Transform Xsmoothlooker;
public Transform Ysmoothlooker;
public float Xspeed = 2f;
public float Yspeed = 2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
looker.LookAt (target);
float YRotation = looker.eulerAngles.y;
float XRotation = looker.eulerAngles.x;
Xsmoothlooker.rotation = Quaternion.Slerp(Xsmoothlooker.rotation , Quaternion.Euler(0 , YRotation, 0), Time.deltaTime * Xspeed);
Ysmoothlooker.rotation = Quaternion.Slerp(Ysmoothlooker.rotation , Quaternion.Euler(XRotation , YRotation, 0), Time.deltaTime * Yspeed);
}
}