- Home /
Rotate turret faster if hull is rotating the same way
Hello, What i want is pretty easy, I have a tank, so a turret and a hull. Both rotate perfectly fine. But the turret's rotation speed is not calculated depending on the hull's rotation speed. Another way of saying it is : both hull and turret rotates at 25 degres/sec no matter what happens. I'd like to make it so when the hull is rotating left and the turret right, the turret stays still (the hull's rotation cancels out the turret's rotation since it's rotating the opposite way). And if the turret is rotating left and the same for the hull, then the turret goes twice faster (hull rotation speed + turret rotation speed)
Here is the code I have at the moment :
The 18 th line is to make the turret rotates where the mouse is pointing with a continuous speed
And here's the code to make the whole tank rotates Note that the Camera is a child of the hull. Turret and Hull are both direct Children of the tank gameobject.
Thanks for any help [2]: /storage/temp/155069-2.jpg
Answer by JxWolfe · Apr 06, 2020 at 11:58 PM
Ok, I've built many a tank game, and from what you said about the tank it works like this:
Tank Gameobejct (empty) has tank script (rotates)
Hull Gameobject (model) doesn't have any scripting (maybe shaders, do they count?)
Turret Gameobject (model)
Camera
This if this is how the tank is laid out, then the script on the tank parent should just need a reference to the turret model and rotate that. If the tank parent is rotating, then the children would rotate as well, meaning that no additional calculations are required.
However, if for some reason you don't want to have your tank laid out that way, or cannot for some reason when you rotate the hull of the tank you could also get a reference to the turret and rotate it as well. (In addition to moving the turret on the controller) This way, if you are moving both tank and turret it would move approperetly. (spelling. and this would act the same as putting the tank in the layout I suggested.
IF you want the tank to keep aiming at the same place, and use the tank turret control only to move the turret, then you could set it up so when you move the tank hull, check if the control to move the turret is active, and if it is, then move it along with the hull. (In addition to moving the turret on the controller) Otherwise, it will keep aiming at the same location.
I hope that helps!
Your answer
Follow this Question
Related Questions
How can i follow an object by 1-axis-rotation? 0 Answers
Rotation for 180 degrees of character 1 Answer
How do I call a function containing a while loop, inside the update function? 2 Answers
How can i rotate the aircraft elevator around the yellow axis up and down ?? as shown in the pic 0 Answers
Is 360° the same as 0°? 2 Answers