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 /
This question was closed Nov 15, 2016 at 02:54 PM by Brylos for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Brylos · Nov 15, 2016 at 01:33 PM · c#charactercontroller

Character Controller can accelerate but cant deccelerate

I've managed to get it so my character controller is able to accelerate when starting to move but it doesn't deccelerate at all when I let go of my xbox joystick. I thought it would work based off of what i've coded but it seems to be dependant on the input. If anybody can give me some advice as to how to get it to deccelerate I would be grateful. Thanks in advance :)

     /////// Move
     if (Player.isGrounded)
     {
         Vector3 Movement = new Vector3(HorizontalSpeed, 0, VerticalSpeed);
         Movement = transform.rotation * Movement;
         Player.Move(Movement * Time.deltaTime);

         // Horizontal

         // Right
         if (Input.GetAxis("Left Joystick Horizontal") >= 1)
         {
             HorizontalSpeed += Time.deltaTime * Acceleration;
         }
         else
         {
             while (HorizontalSpeed > 0)
             {
                 HorizontalSpeed -= Time.deltaTime * Acceleration;
             }
         }

         // Left
         if (Input.GetAxis("Left Joystick Horizontal") <= -1)
         {
             HorizontalSpeed -= Time.deltaTime * Acceleration;
         }
         else
         {
             while (HorizontalSpeed < 0)
             {
                 HorizontalSpeed += Time.deltaTime * Acceleration;
             }
         }

         HorizontalSpeed = Mathf.Clamp(HorizontalSpeed, -10, 10);
         if (HorizontalSpeed > -0.4 && HorizontalSpeed < 0.4)
         {
             HorizontalSpeed = 0;
         }

         // Vertical

         // Forward
         if (Input.GetAxis("Left Joystick Vertical") >= 1)
         {
             VerticalSpeed += Time.deltaTime * Acceleration;
         }
         else
         {
             while (VerticalSpeed > 0)
             {
                 VerticalSpeed -= Time.deltaTime * Acceleration;
             }
         }

         // Back
         if (Input.GetAxis("Left Joystick Vertical") <= -1)
         {
             VerticalSpeed -= Time.deltaTime * Acceleration;
         }
         else
         {
             while (VerticalSpeed < 0)
             {
                 VerticalSpeed += Time.deltaTime * Acceleration;
             }
         }

         if (VerticalSpeed > -0.4 && VerticalSpeed < 0.4)
         {
             VerticalSpeed = 0;
         }
     }
Comment
Add comment · Show 2
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 TreyH · Nov 15, 2016 at 01:37 PM 1
Share

You might be confused about the behavior you'll get from this:

 while (HorizontalSpeed > 0)
avatar image Brylos TreyH · Nov 15, 2016 at 01:51 PM 0
Share

probably lol. I was trying to get it so when i have speed and then let go of the joystick it slowly moves it back to 0. But yeah I probably did it wrong. Would you know a way to get that functionality?

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Landern · Nov 15, 2016 at 02:14 PM

Try removing the while loops which @TreyH was mentioning, if this is in the Update function it gets executed each frame, you're using deltaTime to smooth out the in/decrease of acceleration over time anyways, the while loops will pin the code in the loops until it is greater/less than whatever logic you put in there in a single frame or in the case of FixedUpdate step.

  /////// Move
  if (Player.isGrounded)
  {
      Vector3 Movement = new Vector3(HorizontalSpeed, 0, VerticalSpeed);
      Movement = transform.rotation * Movement;
      Player.Move(Movement * Time.deltaTime);
 
      // Horizontal
      // Right
      if (Input.GetAxis("Left Joystick Horizontal") >= 1)
      {
          HorizontalSpeed += Time.deltaTime * Acceleration;
      }
      else if (HorizontalSpeed > 0)
      {
          HorizontalSpeed -= Time.deltaTime * Acceleration;
      }

      // Left
      if (Input.GetAxis("Left Joystick Horizontal") <= -1)
      {
          HorizontalSpeed -= Time.deltaTime * Acceleration;
      }
      else if (HorizontalSpeed < 0)
      {
          HorizontalSpeed += Time.deltaTime * Acceleration;
      }

      HorizontalSpeed = Mathf.Clamp(HorizontalSpeed, -10, 10);

      if (HorizontalSpeed > -0.4 && HorizontalSpeed < 0.4)
      {
          HorizontalSpeed = 0;
      }

      // Vertical
      // Forward
      if (Input.GetAxis("Left Joystick Vertical") >= 1)
      {
          VerticalSpeed += Time.deltaTime * Acceleration;
      }
      else if (VerticalSpeed > 0)
      {
          VerticalSpeed -= Time.deltaTime * Acceleration;
      }

      // Back
      if (Input.GetAxis("Left Joystick Vertical") <= -1)
      {
          VerticalSpeed -= Time.deltaTime * Acceleration;
      }
      else if (VerticalSpeed < 0)
      {
          VerticalSpeed += Time.deltaTime * Acceleration;
      }

      if (VerticalSpeed > -0.4 && VerticalSpeed < 0.4)
      {
          VerticalSpeed = 0;
      }
  }
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 Brylos · Nov 15, 2016 at 02:25 PM 0
Share

Aww thats aweome! It works beautifully. Thanks man! :)

Follow this Question

Answers Answers and Comments

267 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My Character won't jump 0 Answers

Raycast fall detection and issues with .isGrounded, 1 Answer

Help on how to code crouching? 0 Answers

2D plaformer, Can bug me into wall 2 Answers

How to use Input.GetAxis for character movement? 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