- Home /
2D object look at 3D object
can somebody help me out to achieve this. i m trying to make 2d ui to look at 3d object in 3d space, this code working fine but when player moves in other direction and rotates, rotation is not working properly.. public Transform target;
private void Start()
{
}
void Update()
{
float angle = AngleBetweenVector2(transform.position, target.position);
Debug.Log("angle -- " + angle);
transform.rotation = Quaternion.Euler(0, 0, angle);
transform.localEulerAngles = new Vector3(0f, 0f, transform.localEulerAngles.z);
}
private float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
{
Vector2 diference = (vec2 - vec1);
float sign = (vec2.y < vec1.y) ? -1.0f : 1.0f;
return Vector2.Angle(Vector2.right, diference) * sign;
}
Comment
Your answer
Follow this Question
Related Questions
2D sprite that always faces the player? 1 Answer
Making sprites just rotate and not tilt. 3 Answers
need help regarding rotating arm 0 Answers
Camera Scrolling just track the Charakter if z-Rotation=180 0 Answers
pixel perfect 2d Sprite in 3d world with isometric camera, how to do it right? 1 Answer