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 vFMXv · Apr 06, 2016 at 12:18 AM · c#transform.positionmathf.clamp

Speed exceeds the clamped value by 0.3?

Hello, I'm sorry if this is a simple fix and/or a mess of a script, but I'm very new to C# and Unity. I've been trying to clamp the speed at which my character moves forward between 0 and 4, but it always differs by something like 0.3, causing standstill speed to be -0.3, backwards. Thanks in advance! :)

 public GameObject playerCamera;
 public GameObject mainPlayer;
 float currentSpeed = 0f;
 public float accelerationSpeed = 15f;
 public float decelerationSpeed = 15f;
 public int maximumSpeed = 4;
 public int minimumSpeed = 0;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void FixedUpdate () {

     Debug.Log(currentSpeed);

     transform.rotation = Quaternion.Euler(0, playerCamera.GetComponent<MouseLookScript>().currentYRotation, 0);

     currentSpeed = Mathf.Clamp(currentSpeed, minimumSpeed, maximumSpeed);

     if (Input.GetKey(KeyCode.W))
     {
         currentSpeed += accelerationSpeed * Time.deltaTime;
         transform.position += mainPlayer.transform.forward * currentSpeed * Time.deltaTime;
     }

     if (!Input.GetKey(KeyCode.W))
     {
         currentSpeed -= decelerationSpeed * Time.deltaTime;
         transform.position += mainPlayer.transform.forward * currentSpeed * Time.deltaTime;
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
Best Answer

Answer by Ckboyd89 · Apr 06, 2016 at 04:45 PM

In the current state, you are clamping currentSpeed and then modifying it. This results in currentSpeed actually being able to extend beyond the min and max values by (accelerationSpeed Time.deltaTime) or (decelerationSpeed Time.deltaTime).

In order to fix this issue, you'll need to perform the clamp after you have modified the value of currentSpeed. The changed code below should result in the intended functionality:

 void FixedUpdate () {
 
      Debug.Log(currentSpeed);
 
      transform.rotation = Quaternion.Euler(0, playerCamera.GetComponent<MouseLookScript>().currentYRotation, 0);
 20.
 
      if (Input.GetKey(KeyCode.W))
      {
 25.         currentSpeed += accelerationSpeed * Time.deltaTime;
      }
      else
 30.  {
          currentSpeed -= decelerationSpeed * Time.deltaTime;
      }
 
 currentSpeed = Mathf.Clamp(currentSpeed, minimumSpeed, maximumSpeed);
 transform.position += mainPlayer.transform.forward * currentSpeed * Time.deltaTime;
 ...
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 vFMXv · Apr 06, 2016 at 08:20 PM 0
Share

Thank you so much for explaining and giving a solution. Your help is very appreciated, Ckboyd89.

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

140 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

Related Questions

Need help with limited movement with mathf clamp 2 Answers

How to make an object move in the direction another object is facing on 2 axis? 1 Answer

transform.position assign attempt for 'RCCMainCamera' is not valid. Input position is { NaN, NaN, NaN }. UnityEngine.Transform:set_position(Vector3) 1 Answer

[Solved] Linear movement speed, reset position 1 Answer

Photon Multiplayer : position of client player is not getting synchronized. 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