- Home /
How to not move my character diagonally(2d)
hello, im making a rpg and i don't want to move my character diagonally so what should i do thanks.`
using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed=1f;
void Start ()
{
}
void Update ()
{
if (Input.GetKey (KeyCode.A))
{
transform.position-=new Vector3(speed*Time.deltaTime,0.0f,0.0f);
}
if (Input.GetKey (KeyCode.D))
{
transform.position+=new Vector3(speed*Time.deltaTime,0.0f,0.0f);
}
if (Input.GetKey (KeyCode.S))
{
transform.position-=new Vector3(0.0f,speed*Time.deltaTime,0.0f);
}
if (Input.GetKey (KeyCode.W))
{
transform.position+=new Vector3(0.0f,speed*Time.deltaTime,0.0f);
}
}
} `
Try putting a return;
in each of your if statements (at the end) ? It's just and idea like that, i don't know if it will do what you expect ^^ With this technique you may need to do a if/else if/else if/else if sturcture ins$$anonymous$$d of if/if/if/if. Or maybe the if/else if only will work ^^
you my sir are a awesome guy it worked thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks.
caan u write your comment in the answers so i can vote it.
Answer by fokziu · Apr 16, 2015 at 12:47 PM
Dont do If(key) if(key2) etc. Just do If(A) else if(B) etc. this way if character is going forward it wont go to the sides in the same time.
Answer by enishii · Apr 16, 2015 at 10:49 AM
you can try with a vector3 variable and add for example
press a (-1,0,0) + w(-1,1,0)
gameobject.transform.position+=movevector*time.deltatime*speed;