Script Issue, I Think

I have a tank, with a separate turret, imported from Blender. I created and applied a move.js script to the empty which all the tank parts are under.

#pragma strict

var forwardRate : float = 1;
var turnRate : float = 0.1;


function Update () {

// tank's forward speed and action
var forwardMoveAmount = Input.GetAxis("Vertical") * forwardRate;

// force of the tank's turn
var turnForce = Input.GetAxis("Horizontal") * turnRate;

// rotate tank in action
transform.Rotate(0, turnForce, 0);

transform.position += transform.forward * forwardMoveAmount * Time.deltaTime;

}

Thing is, when I play the game, the tank turns kinda rough, every few degrees there is a blip, almost like it stops and then starts rotating again right away.

Then I created another script to turn the turret.

#pragma strict

function Update () {

if(Input.GetKey("a")){
transform.Rotate(0,0,-1);
}

if(Input.GetKey("d")){
transform.Rotate(0,0,1);
}

}

It is applied to the turret. When I try to turn my turret, the tank turns with it! The tank body turns at a slightly slower rate though.

What could be causing these issues?? Is it because everything is under the same empty? How can I solve it?

It looks to me like you might need to get down and dirty with referencing your tank parts directly. Which means, if they are “children” of your empty game object, you’ll need to reference your parent object – and then find your child objects.

It’s probably turning the whole thing because your script is telling Unity to turn the empty object, and, like you said, everything is under the same empty. So if your script is attached to the empty game object, maybe do something like this:

function Start () {
     //Cache your empty game objects transform
     var thisTransform : Transform = transform;     

     //Cache your tank body's transform
     var tankBody : Transform = thisTransform.Find("whatINamedMyTankBody").transform;

     //Cache your turret's transform
     var turretObject : Transform = thisTransform.Find("whatINamedMyTurretObject").transform;
}

This is assuming your tank body and turret are children of your empty game object, and your script is attached to your empty.
Now in Update you can reference your child objects like this:

function Update () {

  if(myTurretsSelected) {
          turretObject.rotation = whatyouarerotatingby;
  }

  //This will constantly rotate your tank body like crazy
  tankBody.rotation = somerotation;
}

So everything in that code might not be 100%, but hopefully it might help. Good luck :slight_smile:

Thanks, but since I know pretty much nothing about JS, I am at a loss as to were to go from here. I get like 8 errors when I try to run that.

Hmm, sorry to hear that Blenderite. What kind of errors are you getting? Are they mostly nullreference errors or something else?

Also, are you wanting to use javascript or c#?

Don’t give up, keep working at it, and you’ll get it eventually :slight_smile: