- Home /
Can only move forward or back, no left or right.
This is really strange, I am very very new to programming so be patient and bear with me please.
As the title says I can only move my character forward or back and not sideways, the funny thing about this is that I don't want him to move forward or back I want only side ways.
It is the basic 2d space shooter you would expect.
I have tried two sets of code, first is below:
var speed : float = 3; var projectile_prefab: GameObject; function Update () { var translationX : float = Input.GetAxis ("Horizontal") * speed; var translationZ : float = Input.GetAxis ("Horizontal") * speed; translationX *= Time.deltaTime; translationZ *= Time.deltaTime; transform.Translate (translationX, 0, translationZ); if(Input.GetKeyDown("space")) Instantiate(projectile_prefab); }
The other set of code I tried was:
var moveSpeed = 1.0; var turnSpeed = 1.0; function Update() { /* if(Input.GetButtonDown("Jump")) { transform.position.z += 1.0; } */ if(Input.GetButton("Forward")) { transform.position += transform.forward * moveSpeed * Time.deltaTime; } if(Input.GetButton("Backward")) { transform.position += -transform.forward * moveSpeed * Time.deltaTime; } if(Input.GetButton("Left")) { transform.eulerAngles.y += -turnSpeed * Time.deltaTime; } if(Input.GetButton("Right")) { transform.eulerAngles.y += turnSpeed * Time.deltaTime; } } if(Input.GetKeyDown("space")) Instantiate(projectile_prefab); }
I cant understand why either of these don't work. Any help is greatly appreciated.
Answer by sonofbryce · Apr 30, 2013 at 12:16 AM
Unity doesn't like when you try to modify eulerAngles directly so first make a copy of its value into a new Vector3, apply the rotation and then set eulerAngles to it.
rot = transform.eulerAngles;
rot.z += turnSpeed * Time.deltaTime;
transform.eulerAngles = rot;
Also, the turnSpeed of 1 is very low and you might not even notice it if its working -- it's 1 degree a second as is. So set it higher to find out if it's working for you.