Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Anymeese · Oct 21, 2013 at 05:51 PM · animationmovementtranslate

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anymeese · Oct 21, 2013 at 06:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I add Animation to this player movement script? or do I have to create a totally new one? 0 Answers

Problems with movement and animation 1 Answer

Joystick animation on android 0 Answers

scripting for animation 0 Answers

How to limit the transform.Translate distance 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges