- Home /
Tank Turret isn't in a right rotation
Hi this is Fender and I've been experimenting with tank movement but when it comes to the movement of the turret in a slope, it's x and z rotation is wrong.
also if I switched to local Rotation, its all different and not what I want. so here is the main code:
public class TurretRotation : MonoBehaviour
{
public Transform turret;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
turret.rotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y,0).normalized;
}
}
also I'm using ""Cinemachine" and my age is 12 so if you can help me that would be greatly appreciated.
Answer by The-Peaceful · Apr 21, 2021 at 10:45 AM
What you are using are the global rotation variable. I think it should work if you used the local variable ( transform.rotation => transform.localRotation). This would mean that your tanks turret rotates around it's local Y axis rather than the worlds Y axis. There is also localEulerAngles which might be easier than using Quaternion.Euler() to convert it first. Hope it helps! :D
https://docs.unity3d.com/ScriptReference/Transform-localRotation.html https://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html
Answer by fendergame · Apr 22, 2021 at 12:03 AM
can you give me an example cause Im not good at those math. also when I use this code:
turret.localRotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y,0).normalized;
and it also works but if I rotate my tank, it will not follow the camera's y angle
If you have a question about @The-Peaceful posts, comment it under their post, instead of writing an answer.
turret.localEulerAngles = new Vector3(0, cameraRotator.eulerAngles.y);
I think the easiest way to achieve what you are trying to do it as shown above. You don't need to normalize the vector btw, normalizing the euler angles would mean that you can't really rotate at all, because you'd set the length of the vector to 1. Say for example, we have our euler angles Vector3(0f, 270f), normalizing it would set this vector to Vector3(0f, 1f), which probably is why it won't work for you right now :D
https://docs.unity3d.com/ScriptReference/Vector3-normalized.html
Also, normally you'd write the code for this in Update() rather than the FixedUpdate(), because FixedUpdate is mostly for Physics related things and as far as I can gather you are not coding Physics there ;D If you wanna read up on that: https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
Thanks but still it doesn't fix the problem cause the turret will not follow the camera's y angle when the tank is turned
well how do you turn the camera's y angle then? I tested the code I wrote and it worked perfectly fine. I was turning a gameObject, with the camera as a child, like this transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * 130f * Time.deltaTime));
. Maybe you could just show me the code you've got right now?
The cinemachine "Cameras" aren't actual cameras, they have components which overwrite the values of the actual camera, so that really shouldn't matter. If you are able to rotate the camera but the turret doesn't copy it's rotation, then it is probably because a bug in the turret rotation script, which, again, if you show me I might be able to help you out with :)
I could but i cant put a GIF image here but i'll try
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Player making a 360° looping, camera not following 0 Answers
How to move an object a fixed distance along one of its axis 1 Answer
Is it possible to use a quaternion from a gameobjects position to another gameobjects position? 1 Answer
easing camera rotation after mouse up? 0 Answers