Question by
chaosninja7 · Oct 26, 2016 at 06:24 PM ·
2d character
2D spaceship won't move. Help.
So my little speec shep won't move idk why. Code below.
#pragma strict
public var ship_speed: float = 0;
var max_speed: float = 100;
var ship_acceleration: float;
var ship_deceleration: float;
function Update ()
{
Movement();
}
function Movement()
{
if(Input.GetAxis("Vertical") && ship_speed < max_speed)
{
ship_speed += 1;
transform.Translate(Vector3.up * ship_speed);
}
}
Comment
Answer by conman79 · Oct 26, 2016 at 07:17 PM
There are few problems I see with your script but the reason it won't move is Input.GetAxis("Vertical") doesn't return a boolean, it returns a float.
Presuming you want to move the ship if the vertical axis is positive, change your code to this:
if(Input.GetAxis("Vertical") > 0 && ship_speed < max_speed)
{
ship_speed += 1;
transform.Translate(Vector3.up * ship_speed);
}
Now you'll probably find your ship moves way too fast. That's because you are moving it by huge amounts every frame. To take framerate into account you should use Time.deltaTime like this:
if(Input.GetAxis("Vertical") > 0 && ship_speed < max_speed)
{
ship_speed += 1;
transform.Translate(Vector3.up * ship_speed * Time.deltaTime);
}