- Home /
Cancel a transform.translate in progress?
 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour
 {
     //NEXT TO DO!  Make movement immediately change directions/stop with user commands, not keep going for a bit THEN change
     
     public float walkSpeed = 3.5f;
     public bool moving = true;
     public bool snapToGrid = false;
     public float offset = 0.5f;
     public char lastDirection = 'n';
     
     //these 3 used for positioning back onto the grid
     float x;
     float y;
     float z;
     
     // Use this for initialization
     void Start ()
     {
     }
     
     // Update is called once per frame
     void Update ()
     {
         if(Input.GetAxis("Vertical") > 0.0) //w
         {
             if(lastDirection == 'a' || lastDirection == 's' || lastDirection == 'd')
                 reset();
             lastDirection = 'w';
             transform.rotation = new Quaternion(0,0,0,0);
             if(moving)
                 transform.Translate(0,0,walkSpeed * Time.deltaTime);
             animation.CrossFade ("run");
         }
         else if(Input.GetAxis("Vertical") < 0.0) //s
         {
             if(lastDirection == 'w' || lastDirection == 'a' || lastDirection == 'd')
                 reset();
             lastDirection = 's';
             transform.rotation = new Quaternion(0,1,0,0);
             if(moving)
                 transform.Translate(0,0,walkSpeed * Time.deltaTime);
             animation.CrossFade ("run");
         }
         else if(Input.GetAxis("Horizontal") > 0.0) //d
         {
             if(lastDirection == 'w' || lastDirection == 'a' || lastDirection == 's')
                 reset();
             lastDirection = 'd';
             transform.rotation = new Quaternion(0,1,0,1);
             if(moving)
                 transform.Translate(0,0,walkSpeed * Time.deltaTime);
             animation.CrossFade ("run");
         }
         else if(Input.GetAxis("Horizontal") < 0.0) //a
         {
             if(lastDirection == 'w' || lastDirection == 's' || lastDirection == 'd')
                 reset();
             lastDirection = 'a';
             transform.rotation = new Quaternion(0,-1,0,1);
             if(moving)
                 transform.Translate(0,0,walkSpeed * Time.deltaTime);
             animation.CrossFade ("run");
         }
         else
         {
             reset();
         }
     }
     
     void reset()
     {
         transform.Translate(0,0,0);
         animation.CrossFade ("idle");
         
         //put character back onto the grid -- have to do - as well as + because + doesn't work correctly when the position value is negative
         if((int) transform.position.x >= 0)
         {
             x = transform.position.x - 0.5f; //this way instead of 10.1 -> 10 and 10.6 -> 11, all 10 values will go to 10, because 10.1 -> 9.6 -> 10, 10.6 -> 10.1 -> 10
             x = Mathf.RoundToInt(x) + offset;
         }
         else
         {
             x = transform.position.x + 0.5f;
             x = Mathf.RoundToInt(x) - offset;
         }
         
         if((int) transform.position.z >= 0)
         {
             z = transform.position.z - 0.5f;
             z = Mathf.RoundToInt(z) + offset;
         }
         else
         {
             z = transform.position.z + 0.5f;
             z = Mathf.RoundToInt(z) - offset;
         }
         
         y = transform.position.y;
         
         transform.position = new Vector3(x,y,z);
     }
     
     void OnCollisionEnter(Collision col)
     {
         if(col.gameObject.tag == "Wall")
             moving = false;
     }
     
     void OnCollisionExit(Collision col)
     {
         if(col.gameObject.tag == "Wall")
             moving = true;
     }
 }
Right now I have a "game" with a top-down view of a box and a character running in it. What I'm TRYING to do is keep the character on a grid, which is why whenever he stops running I round his position ont he x-z plane to n.5 (so 10.2 goes to 10.5, 9.8 goes to 9.5, etc). However I'm running into a problem: Whenever i try to change directions immediately, rather than go one direction, wait..... then go another direction, it won't immediately change. Instead it keeps running for a little bit, THEN changes. So how can I stop the movement in mid-movement? I tried to "overwrite" it with the (0,0,0) line at the top of Reset, but that didn't help
Thanks for any/all help!
Answer by Owen-Reynolds · Oct 21, 2013 at 06:01 PM
GetAxis has a built-in slowdown. It's a special feature meant to look like a real guy reversing direction or stopping. It also ramps up the value -- if you lean on W, getAxis Vert goes from 0 to 1 over a few frames, then gradually drops to 0 when you let go.
You can change the speeds with the Input Manager settings (there's a "snap reverse direction" toggle, which maybe is off for you?)
But, if you never want that at all, just read the keys directly: if(Input.GetKey("w"))...
Also, FYI, Translate takes the amount to move. So Translate(0,0,0) does nothing. It adds (0,0,0) to where you are. 
Thanks for the help! I eventually plan to play with a controller, but I want to eli$$anonymous$$ate the gradual speed up/down so it's a fast-paced game. I'm just testing it with wasd since that's easier than using my roommate's controller for now while I'm in early stages of development
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                