Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Gabestronaut · Jan 21, 2016 at 05:59 AM · movementinputspeedcharacter controllercontrols

How can I make character accelerate and deccelerate using GetAxis?

I want to make my own character to controller. Sure the default is great BUT I want to learn how to do my own and understand it by doing. My question is how to make a character accelerate and deccelerate using get axis. I can manage doing it using OnGetKey(Blah) but on GetAxis is a blur. Not really sure how it works on inputs. So How do I refer to it when a player press a key, and how to do I refer to it when a player lets go of a key. Along that is managing speed in between all that.

Here is what I managed to do but I do not know where to go from there. Read on forums about mathf.clamp and smoothDamp but I need a solid understanding of the GetAxis use and how to go about it. I will provide code below so you can see what Im trying to do with it. This code accelerates properly but does so without waiting for input. So it needs to work on input and likewise backwards for decceleration. Thanks Everyone!

FYI: I will probably ask this later unless someone viewing may know the answer, how to tackle slopes once I do this.

 {
 
     private float xMove;
     private float zMove;
     public float speed;
     public float currentSpeed = 0;
     public float acc;
 
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         movementInput();    
     }
 
     void movementInput()
     {
         if (currentSpeed < speed)
         {
             currentSpeed += acc * Time.deltaTime;
         }
         xMove = Input.GetAxis("Horizontal") * currentSpeed * Time.deltaTime;
         zMove = Input.GetAxis("Vertical") * currentSpeed * Time.deltaTime;
 
         transform.Translate(new Vector3(xMove, 0, zMove));
     }
 } 

Comment
Add comment · Show 1
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 Gabestronaut · Jan 22, 2016 at 04:53 AM 0
Share

Ive made some progress and managed to decipher input.getaxis a little more. I also tested this with joysticks. So Here is my code to figure out whats doing what and some explanations of my findings. If this helped anyone and was able to proceed please let me know. I will help as I make more progress but if anyone makes progress quicker we can all solve this problem faster. THAN$$anonymous$$S!

  private float x$$anonymous$$ove;
     private float z$$anonymous$$ove;
     public float speed;
     
     // Update is called once per frame
     void Update () 
     {
         movementInput();    
     }
 
     void movementInput()
     {
 
         x$$anonymous$$ove = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime; //Inputs for up and down
         z$$anonymous$$ove = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime; //Inputs for left and right
 
         transform.Translate(new Vector3(x$$anonymous$$ove, 0, z$$anonymous$$ove)); //move my object upon input
 
         //Input.GetAxis are Values between 1- and 1. 
         //Vertical means 1 is up and -1 is down 0 is no action. It also takes joystic zones into account. IE: half way up on joystic is 0.5
         //Input.GetAxis here uses > or < to take into account slight movement of Joystick. Other wise using == 1/ == -1 / == 0 would ignore slight joystick movement
 
         //NOTE:Input.GetAxis VS Input.GetAxisRaw
         //Input.GetAixs mimics joystick movements on keyboard and gradually works up to 1 or -1. Can cause issues creating acceleration and decceleration mimmicking slow joystick movement to full press with additional drag. It seems like drag but isn't.
         //Input.GetAxisRaw immediately goes to desired speed
 
         //NOTE: == 0 on both vertical and Horizontal execute at the same time. So leaving one would probably be best because I do not want any future parameters executing twice
 
 
         //-------------VERTICAL $$anonymous$$OVE$$anonymous$$ENT-----------------
         if (Input.GetAxisRaw("Vertical") > 0)
         {
             print("Up");
         }
         if (Input.GetAxisRaw("Vertical") < 0)
         {
             print("Down");
         }
         if (Input.GetAxis("Vertical") == 0)
         {
             //print("Unpressed Vertical");
         }
 
         //--------------HORIZONTAL $$anonymous$$OVE$$anonymous$$ENT---------------
 
         if (Input.GetAxisRaw("Horizontal") > 0)
         {
             print("Right");
         }
         if (Input.GetAxisRaw("Horizontal") < 0)
         {
             print("Left");
         }
         if (Input.GetAxis("Horizontal") == 0)
         {
             //print("Unpressed Horizontal");
         }
 
     }

0 Replies

· Add your reply
  • Sort: 

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

43 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 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 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 move a game object/camera along a defined path with a speed defined by input of a (bluetooth) keyboard? 0 Answers

Touch Controls without virtual joystick 0 Answers

8-way 2D top down movement (diagonal) idle animation issues 0 Answers

Roll a Ball Tutorial issue 0 Answers

Character Cannot Move from its Spot 0 Answers


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