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 LeftyTwoGuns · Oct 14, 2013 at 07:57 PM · movementaxismathf.clampboundary

Clamping movement range on X-axis

I have a vehicle that moves left or right on the X-axis using wheel colliders but I need to restrict it to a certain range. I'm using Mathf.clamp because apparently it doesn't cause the rigidbody to jitter like other methods do. Here is my script which gives a couple of errors:

 void FixedUpdate(){
 
 gas = Input.GetAxis("Horizontal");
         
         frontLeftWheel.motorTorque = enginePower * gas;
         frontRightWheel.motorTorque = enginePower * gas;
         rearLeftWheel.motorTorque = enginePower * gas;
         rearRightWheel.motorTorque = enginePower * gas;
         frontRightMiddle.motorTorque = enginePower * gas;
         frontRightBackMiddle.motorTorque = enginePower * gas;
         frontLeftMiddle.motorTorque = enginePower * gas;
         backLeftMiddle.motorTorque = enginePower * gas;
 
 
 Vector3 temp = new Vector3(-xBoundry, 0, 0);
         temp.x = Mathf.Clamp(temp.x, -18.2, 18.2);
         transform.position = temp;

I understand basically how Mathf.Clamp works. You state the value and then its range between two floats, then you store the changing transform in a temporary variable and reassign it (which I think I'm going wrong). But I'm confused what the value here would be. Is it the transform position or is it the Input?

Comment
Add comment · Show 4
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 robertbu · Oct 14, 2013 at 08:02 PM 1
Share

I'm guessing you want:

    Vector3 temp = transform.position;
    temp.x = $$anonymous$$athf.Clamp(temp.x, -18.2f, 18.2f);
    transform.position = temp;
avatar image LeftyTwoGuns · Oct 14, 2013 at 09:36 PM 0
Share

I'm getting two errors for the second line of the clamp script:

"The best overloaded method match for `UnityEngine.$$anonymous$$athf.Clamp(float, float, float)' has some invalid arguments"

and

"Argument '#2' cannot convert 'double' expression to type 'float'"

avatar image robertbu · Oct 14, 2013 at 09:56 PM 1
Share

By default in C#, floating point numbers are doubles. Unity uses floats (a lower precision version of floating point numbers). In order to declare floats, you need to add a 'f' suffix. I edited my comment to fix the problem.

avatar image LeftyTwoGuns · Oct 14, 2013 at 09:59 PM 0
Share

Damn, that's my fault I should've noticed that. Thanks! Got it working

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

14 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

Related Questions

Little problem with Mathf.Clamp as used in Space Shooter Tutorial 8 Answers

Restric the script and movement in specific area 0 Answers

Axis Issue 1 Answer

Z axis change 2 Answers

How to make 2D Movement with only 1 Axis at the same Time,How to make 2d Movement on 1 Axes at the same time? 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