- Home /
lock turret rotation to local Y
I have this basic turret script: void Rotate() {
Vector3 pointer = cam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(pointer, cam.transform.forward, out hit))
{
Vector3 direction = hit.point - transform.position;
direction.y = 0.0f;
float singleStep = rotationSpeed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, direction, singleStep, 0.0f);
transform.rotation = Quaternion.LookRotation(newDirection);
}
}
All it is doing to keep the turret flat is by setting direction.y = 0. However, I am planning on adding terrain to my project, and the turret currently will always stay flat regardless of the tank's rotation. I have tried a multitude of things such as making a child gameobject in front of the tank:
if (Physics.Raycast(pointer, cam.transform.forward, out hit))
{
Vector3 direction = hit.point - transform.position;
Vector3 tankDirection = tankAngle.position - tank.position;
direction.y = tankDirection.y;
float singleStep = rotationVelocity * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, direction, singleStep, 0.0f);
transform.rotation = Quaternion.LookRotation(newDirection);
}
I've also messed around with the pythag theorem to move the vector 3, but nothing worked. I don't like asking for the answer, but I'm truly stuck and I have a feeling there is something obvious I'm missing.
Answer by UnityedWeStand · Jul 28, 2020 at 12:17 AM
No shame in asking for help, as it does take a bit of mathematical trickery to get the result you desire.
Try an implementation using Quaternion.AngleAxis()
. The axis argument will be whatever vector defines the turret's local up direction (95% of the time, it is transform.up
). The angle argument is a bit more complicated.
First, Identify the turret's local axis that is in the "forward" direction (95% of the time, it will be transform.forward
). Then, "flatten" that direction vector onto the xz-plane in the same manner as your existing direction
. We will call this vector myDirection
.
Although it seems like we could just use Vector3.Angle()
to return the angle between these vectors, we actually need to go a more complicated route. Vector3.Angle()
always returns a non-negative value, but Quaternion.AngleAxis()
needs both positive and negative angle values to determine whether to rotate right or left respectively. Fortunately, we can use properties of the cross product between vectors to get this signed angle value. Long story short, float angle = Vector3.Angle(myDirection, direction) * Mathf.Sign(Vector3.Cross(myDirection, direction).y)
. The order of arguments in Vector3.Cross()
matters, and I believe the order I listed is correct. If you find your turret rotating opposite to what is expected, just switch the arguments here.
Your turret's new rotation should be transform.rotation = Quaternion.AngleAxis(angle, localUp) * transform.rotation
@UnityedWeStand Thanks for this! I looked farther into Quaternion.AngleAxis and Vector3.cross and it makes much more sense. However, using Quaternions takes away my ability to use Vector3.RotateTowards. Is there away around this or will I have to use something like Quaternion.Lerp?
Yep, in this case you would need to store the turret's original rotation and do something like transform.rotation = Quaternion.Slerp(Quaternion.identity, Quaternion.AngleAxis(angle, localUp), t) * originalRotation
. Slerp will be preferable to Lerp especially if the rotations are going to be large.
Answer by deniskotpletnev · Jul 27, 2020 at 10:12 PM
Try attaching the Rigidbody to the turret and blocking the y-axis rotation. Don't forget to assign IsKinematic = true and IsGravity = false
Rigidbody constraints don't do anything as a script is rotating the object
Your answer
Follow this Question
Related Questions
What is wrong with this rigidbody? 2 Answers
How to make the calculated angle respective of the players rotation? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers