Search Unity

My Tank moving script

Discussion in 'Scripting' started by nani2say, Apr 19, 2013.

  1. nani2say

    nani2say

    Joined:
    Apr 18, 2013
    Posts:
    4
    Hi,
    I have used a script for the movement of the tank that i have desined. But the script moves the tank in a horizontal orientation. It does not move the tank in the direction pointed by its gun. it moves in the sideward direction.
    Any suggestions on how to solve this?

    Below is the script i used which i have found from unity forums:
    ===============================================
    var speed:float=50; //speed of the car, tweek as needed based on your scale
    var turnSpeed:float=180; //turn speed

    function Update()
    {
    //grab the input axes
    var steer=Input.GetAxis("Horizontal");
    var gas=Input.GetAxis("Vertical");

    //if they're hittin' the gas...
    if (gas!=0)
    {
    //take the throttle level (with keyboard, generally +1 if up, -1 if down)
    // and multiply by speed and the timestep to get the distance moved this frame
    var moveDist=gas*speed*Time.deltaTime;

    //now the turn amount, similar drill, just turnSpeed instead of speed
    // we multiply in gas as well, which properly reverses the steering when going
    // backwards, and scales the turn amount with the speed
    var turnAngle=steer * turnSpeed * Time.deltaTime * gas;

    //now apply 'em, starting with the turn
    transform.rotation.eulerAngles.y+=turnAngle;

    //and now move forward by moveVect
    transform.Translate(Vector3.forward*moveDist);
    }

    }
     
  2. dendens2

    dendens2

    Joined:
    Jan 4, 2013
    Posts:
    8
  3. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    well firdt off alots of question come to me. That it need.

    Are the gun and tank split apart?
    is the gun attached to the tank so that it a child of of the tank.
    Did you attach the script to the gun?

    For me it seem like you attached the scirpt to Gun, and turning and moving this.
     
unityunity