question about turret on tank

Hello! i have question about my game. When i am driving my tank on mountain, and i am rotating my turret it is twisting in different directions, how can i fix it? here is script i am using: `#pragma strict

var speed : float = 5;
var backspeed : float = -5;

function Update() {

if (Input.GetKey(“z”))
{
transform.Rotate(0,speedTime.deltaTime, 0,Space.World);
}
if (Input.GetKey(“x”))
{
transform.Rotate(0,backspeed
Time.deltaTime, 0,Space.World);
}

if (Input.GetKey(“d”))
{
transform.Rotate(0,speedTime.deltaTime, 0,Space.World);
}
if (Input.GetKey(“a”))
{
transform.Rotate(0,backspeed
Time.deltaTime, 0,Space.World);
}
}`
and screenshot:

It looks like you are using Space.World when you should be using Space.self Space.World will apply the rotations around the worlds axis alignment (typically the usual axis-aligned setup of Y is up). Space.self will apply the rotations around the local axis alignment of the object e.g your tanks up vector will always be pointing out of the top of the tank using this. Give it a try and see if it works

1 Answer

1

You need to rotate the turret around its local axis

if (Input.GetKey("z")) { 
transform.Rotate(0,speed*Time.deltaTime, 0,Space.Self); 
}