Hi, I got a weird question here…
In my game I have a tank which when the scripts are enabled will aim up at the player (which is in a plane btw) I wrote a script using quaternions that makes the base of the tank rotate only on it’s y axis (which is left and right for it) by using LookRotation() and setting x and z to 0 in function Update. While the base looks at the player, I also want the barrel to move only on it’s z axis (which is up and down for it). The script works the same way as the first one.
Unfortunately, it can’t have it’s y axis stuck at zero because it won’t rotate with the base which it is a child of, but if I don’t it does weird things.
So.
Is there a simpler way to do this besides locking the axis, or can you lock it completely locally? Or is there a better way to do it all together?
here are the two scripts:
for the base:
var target : Transform;
function Update () {
var theRotation = Quaternion.LookRotation(target.position - transform.position, Vector3.forward);
theRotation.x = 0.0;
theRotation.z = 0.0;
transform.rotation = Quaternion.Lerp(transform.rotation, theRotation, Time.deltaTime * 20);
}
Now that I look at it, the code for the barrel is the same, yet it only works with the base, and yes I checked, I do want to rotate on the z axis for that.
Sorry if this is a nooby question or if this is already answered somewhere, I looked for it a good while. Thanks!
I’m not exactly sure what your asking, but this is how I’d set up a tank/turrent heirarchy.
- Tank ← Tank script goes here, with no renderers/meshes/etc
-
- Cannon ← Cannon mesh and child components go here
-
-
- Blowback ← Model for portion of cannon that blows backward
-
- Base ← Base mesh and child components go here
-
-
- Treads ← Wheels and treads model animates with the base
(Excuse my poor formatting skills…)
So essentially, you separate the Base from the Cannon entirely. This is much cleaner setup than having the cannon under the base, and having it constantly set its world-space rotation to a value. You’ll get jittering effects and its generally a bad idea.
Now the Tank script can have pointers to all its sub-components to control everything. You can separate it further by placing a Treads script on the Treads, that is independent of the Tank. That way you could reuse that code and place it on a car, or w/e. Similiarly, you could have the Cannon script be independent, so you could reuse it on a Turret unit.
Hope this helps!
Hey look I’m answering my own question lol
Thanks for your help guys and sorry if I didn’t explain it well enough but the x axis of the base and the y axis of the barrel work and still stay in their rightful place if the tank starts going over a bump or flipping over or something. This is the code I came up with:
(This is just for the base but it’s the same just with the axis changed for the barrel)
var target : Transform;
var targetRotation : Quaternion;
function LateUpdate () {
targetRotation = Quaternion.LookRotation(target.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 1);
transform.localEulerAngles.x = 0;
transform.localEulerAngles.z = 0;
}
This is my first question on answers btw so yeah. Although Answers has rly helped me before, just reading answers, because I’m a kid so I’m completely self taught and this is like the best place to go.