简体   繁体   中英

Unity Angle Between Positions

I am currently working on an Among Us clone. I was working on a venting system where the arrows (to navigate through the vents) are pointing in a completely wrong direction.

The arrow-prefab is just an empty object with the sprite as a child with its position changed along the forward axis.

Vector3 targetDir = myVent.transform.position - nextVent.transform.position;
transform.eulerAngles = new Vector3(0, Vector3.SignedAngle(myVent.transform.position, targetDir, Vector3.forward), 0);

The Whole Scene: Screenshot 1

How the Arrow Prefab looks like: Screenshot 2

Thank you!

Use cross products to find the forward direction the transform should be, then use Quaternion.LookRotation to set the rotation:

Vector3 right = nextVent.transform.position - myVent.transform.position;

Vector3 forward = Vector3.Cross(right.normalized, Vector3.up);

transform.rotation = Quaternion.LookRotation(forward);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM