- Home /
Turret on Tank rotating fine until I'm not flat on ground
I have a TANK with a Turret on top, the turret follows my mouse around the screen and it rotates perfectly. And the turret never clips into the tank thanks to x and z being zerod out. When my tank is climbing a hill though the turret goes right through the tank. I think it has something to do with local angles or something but I can't figure it out.
SEE IT HERE - http://dl.dropbox.com/u/24020723/GAME/TANK.html Take the tank onto a hill and see what happens to turret Thanks!
Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
float distance;
if( zeroPlane.Raycast( ray, out distance ) )
{
Vector3 outputPosition = ray.origin + ray.direction* distance;
var newRotation = Quaternion.LookRotation(outputPosition-turretRotate.position).eulerAngles;
newRotation.x = 0;
newRotation.z = 0;
turretRotate.rotation = Quaternion.Euler(newRotation);
}
It is the child of the tank - added link to see it in action - thanks for looking
A shot: turretRotate.Rotate(0,turretRotate.eulerAngles.y+newRotation.y,0,Space.Self)
i haven't thought about this much but would it make a difference if you set the localRotation ins$$anonymous$$d of rotation
Answer by bernardfrancois · Jan 20, 2013 at 09:05 AM
I ran into the same issue when doing a tank with turret before. You'll need to use Transform.localRotation in order to use the local object's up / Y axis in the Quaternion.AngleAxis method:
Transform turretTransform = transform.Find ("Turret");
turretTransform.localRotation = Quaternion.AngleAxis (turretAngle, Vector3.up);
Looks obvious, but still took a while to figure out.
Note that you may have to change Vector3.right to Vector3.up or Vector3.forward in this line of code, depending on the axis containing the longest direction (I'd recommend reexporting the object though, as it would make it more intuitive while programming).
Sorry I want to make sure I understand, how do I get turretAngle? if you don't $$anonymous$$d can you show example with my variable names? I'm a complete coding noob, thanks so much for your help
turretAngle could be a variable you'd have added to your class, which would contain the angle. For instance when you press certain buttons, you'd increase or decrease this variable.
If you don't understand this, I suggest reading more about the program$$anonymous$$g language you're using, and also reading the documentation on the Input class in the Unity scripting reference.
Good luck with it!
Your answer
Follow this Question
Related Questions
Rotation with a slider 2 Answers
Is there no way to get reliable angles from a rigidbody? 2 Answers
How make child object rotate inverse to its parents rotation ? 1 Answer
apply a modified rotation based on another objects' rotation 1 Answer
Why is Quaternion.Eular only rotating in one direction? [Answered] 1 Answer