Question by
WishPotato · Apr 19, 2016 at 02:29 PM ·
rotationgameobjectturret
Object rotation problem
I am currently working on a "turret" for my game, which is a 3D sidescrolling platformer, though I have a problem with it's rotation, which can we seen through this gif.
As shown it cuts, and does not extent the rotation further than 180 degrees.
I am rotating it by following code:
targetRot = Quaternion.LookRotation(transform.position - lastPos.position);
targetRot.x = 0.0f;
targetRot.y = 0.0f;
transform.rotation = Quaternion.SlerpUnclamped(transform.rotation, targetRot, rotationSpeed * Time.fixedDeltaTime);
Any method for a proper rotation, which leads to the turret rotating the whole way round?
Comment
Best Answer
Answer by WishPotato · Apr 19, 2016 at 03:12 PM
Fixed it myself, by doing following instead:
Vector3 direction = transform.position - player.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.fixedDeltaTime * rotationSpeed);