- Home /
Question by
j_pouya1999 · Jun 03, 2016 at 06:13 AM ·
unity 52dobjectlookat
2d obj look at limit in unity
I want the y-axis limits And Max and Min give Unknown
public Transform target; public GameObject player; public enum FacingDirection { UP = 100, DOWN = 90, LEFT = 100, RIGHT = 0 }
void Update()
{
//LookAt2D(target);
LookAt2D(target, 17f, FacingDirection.RIGHT);
}
// Other movement needs to be calculated first, so
// we use LateUpdate() instead of Update().
void LookAt2D(Transform theTarget, float theSpeed, FacingDirection facing)
{
Vector3 vectorToTarget = theTarget.position - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
angle -= (float)facing;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * theSpeed);
}
Comment