Quaternion.RotateTowards wierd issues
I have a script for a turret to rotate so it is facing a position, called "Target". I don't know how to explain what is happening, so below is the video of what's going on with my turret.
Video (Google Photos) - Sorry for the music forgot to turn it off :P
I have it so the turret's target should always be the red "target indicator" that I have on there, and the turret is only making a 180 degree rotation clockwise and nothing else.. Here's the rotating script
GameObject LookAtCalculations = transform.Find("TurretRotating").gameObject;
LookAtCalculations.transform.position = transform.position;
LookAtCalculations.transform.LookAt(TargetPosition, Vector3.up);
Quaternion GoalRotation = LookAtCalculations.transform.rotation;
GoalRotation.x = 0;
GoalRotation.z = 0;
GoalRotation.w = 0;
transform.rotation = Quaternion.RotateTowards(transform.rotation, GoalRotation, TurretSpeed * Time.deltaTime);
I have no idea what's happening and how to make the turret simply look at the target, or where the player is aiming, so any and all help appreciated.
Answer by SunnyChow · Jan 20, 2017 at 03:21 AM
Vector3 lookDir = TargetPosition - transform.position;
lookDir.y = 0;
Quaternion GoalRotation = Quaternion.LookRotation (lookDir);
Don't directly set the value of a Quaternion unless you are really good at math
Right ^^. As always for reference i link this video. After watching it you should either understand quaternions or you better not tinker with it's components ^^.
Thanks! I should probably change them over to euler angles before I edit them.
Your answer
Follow this Question
Related Questions
How to Rotate on Y Axis by 90 degrees left or right? 1 Answer
Look Rotation Viewing Vector is Zero 0 Answers
Rotating camera not working? 0 Answers
Annoying Quaternions and Eulers 1 Answer