- Home /
Disable Diagonal Move
Hey guys, I'm doing a simple move and want to disable the diagonal move.
I don't have any idea how to do that. I'm using transform.Translate
This is the code
  public void Update()
         {
             if (Input.GetKey(KeyCode.RightArrow))
             {
                 transform.Translate(Vector3.right *5 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.LeftArrow))
             {
                 transform.Translate(Vector3.right * -5 * Time.deltaTime);
             }
 
             if (Input.GetKey(KeyCode.UpArrow))
             {
                 transform.Translate(Vector3.forward* -5 * Time.deltaTime);
             }
             if (Input.GetKey(KeyCode.DownArrow))
             {
                 transform.Translate(Vector3.forward * 5 * Time.deltaTime);
             }
         }
Answer by Kibsgaard · May 14, 2013 at 06:57 PM
So basically, you want to only be able to move in one direction at a time?
Easiest solution is just to add an 'else' between each of your if-statements.
Answer by ExTheSea · May 14, 2013 at 06:54 PM
I would suggest you to do this:
      public void Update()
     {
     if(Input.GetAxis("Vertical")==0){
     if (Input.GetKey(KeyCode.RightArrow))
     {
     transform.Translate(Vector3.right *5 * Time.deltaTime);
     }
     if (Input.GetKey(KeyCode.LeftArrow))
     {
     transform.Translate(Vector3.right * -5 * Time.deltaTime);
     }
     }
     if(Input.GetAxis("Horizontal")==0){
     if (Input.GetKey(KeyCode.UpArrow))
     {
     transform.Translate(Vector3.forward* -5 * Time.deltaTime);
     }
     if (Input.GetKey(KeyCode.DownArrow))
     {
     transform.Translate(Vector3.forward * 5 * Time.deltaTime);
     }
     }
     }
Warning: This code is untested but in theory it should work.
Hope it helps.
Your answer
 
 
             Follow this Question
Related Questions
Wait a fraction of a second within a Function Update() 1 Answer
How to have a GameObject follow a path with c#? 3 Answers
I need to travel the same distance in less than 1 second 1 Answer
Moving a GameObject towards a CharacterController 1 Answer
How do you make bullets face the direction it's going? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                