I’m working on a top-down 2D game but can’t quite get a sprite to rotate towards any given vector. So far this is the best code I’ve got for it but the sprite will not look to the left side of the it, only the right:
transform.LookAt(WorldPos);
transform.rotation = new Quaternion(0,0,transform.rotation.z, transform.rotation.w);
WorldPos is the point I’m getting from ScreenToWorldPoint. I also have to force the rotation for x and y to 0 so that you can still see the sprite. Anybody got anything better for rotating a sprite toward an object in 2D? I’m thinking it should be this hard.
Here something better. I’ve posted it several times, but as usual searching for the answers is not turning up the ones I want:
var dir = WorldPos - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Note this code assumes that the ‘forward’ side of your sprite is the right side. If it is another side (like up), you will need to adjust the angle (subtract 90) before passing it to the AngleAxis() function;
I have enemy ships in a top down shooting game with very similar behavior. The following is a script that can be attached to a GameObject. It has a public variable for the target your are wanting your object to face, and the update behavior sets the rotation.
using UnityEngine;
using System.Collections;
public class FaceTarget : MonoBehaviour
{
public Transform target;
private Vector3 v_diff;
private float atan2;
void Update()
{
v_diff = (target.position - transform.position);
atan2 = Mathf.Atan2 ( v_diff.y, v_diff.x );
transform.rotation = Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg );
}
}
You can programatically add this component and set its target variable as well. If your object is rotated already, you will have to add or subtract an offset from atan2 to compensate for the objects initial rotation.
There is a simpler and cleaner way to do this than in “Best answer”:
`Vector3 relativeTarget = (target.position - transform.position).normalized;
//Vector3.right if you have a sprite rotated in the right direction
Quaternion toQuaternion = Quaternion.FromToRotation(Vector3.right, relativeTarget);
transform.rotation = Quaternion.Slerp(transform.rotation, toQuaternion, rotationSpeed * Time.deltaTime);`
public int playerSpeed = 5;
private float rotationx;
private float rotationy;
private Vector3 touchcoordinates;
private Transform myTrans;
private bool RestartButton_Bool;
public Transform background_map;
public Touch touch1;
public GameObject GameoverText;
void Start ()
{
//Caching of the variables
myTrans = this.transform;
}
// Update is called once per frame
void Update ()
{
//Keep the character without any rotation
myTrans.rotation = Quaternion.Euler(0,0,0);
//Check to see if the app is running over iOS or Android Devices
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android)
{
//Get touch data
foreach (Touch touch in Input.touches)
{
touch1 = touch;
touchcoordinates = touch.position;
//Coverting touch coordinates in accordance with game use.
Ray ray = Camera.main.ScreenPointToRay(touchcoordinates);
transform.LookAt(ray.GetPoint(-1000),Vector3.forward);
}
touchcoordinates = touch1.position;
}
//Check if the app is ruuning anywhere other than Mobile devices
else
{
//Coverting touch coordinates in accordance with game use.
Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
transform.LookAt(ray1.GetPoint(-1000),Vector3.forward);
}
//Moving the character forward
transform.Translate(Vector2.up * Time.deltaTime * 5);
}