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 Digital-Phantom · Mar 27, 2015 at 02:16 PM · rigidbodyvector3axisrotatingmathf.clamp

Mathf.Clamp having weird effects on player object with rigidbody

Had a fairly simple little script that simply used Input.GetAxis horizontal to move my player object left and right. All was good until I decided I needed to put a limit on the amount of movement along the x axis.

After adjusting my script with some Mathf.Clamp coding I'm getting some weird effects. Basically its rotating my playerObject 180 degrees around the x axis and 180 degrees around the y axis. (affectively turning it upside down and facing rearwards)

     public int Min = 30;
     public int Max = 30;

     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0);
         GetComponent<Rigidbody>().velocity = movement * speed;
 
         GetComponent<Rigidbody>().position = new Vector3(Mathf.Clamp (GetComponent<Rigidbody>().position.x, Min, Max), 0, transform.position.z);
         
     }
 

Any ideas/suggestions to as why this is happening? The actual clamping/restriction on the x axis is working fine, but this damn rotation is making my head spin (excuse the pun....lol)

???

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 Digital-Phantom · Mar 27, 2015 at 02:43 PM 0
Share

O$$anonymous$$... I've fixed it by changing line 10 to this -

 GetComponent<Rigidbody>().position = new Vector3($$anonymous$$athf.Clamp (GetComponent<Rigidbody>().position.x, $$anonymous$$in, $$anonymous$$ax), transform.position.y, transform.position.z);
 
 

(changed the 0 to transform.position.y)

So the problem is fixed, but that still doesn't really help me learn how/why the problem existed in the first place. What exactly was it about the fixed version of my script that was correct compared to the non-working version ?

Any clarification on this would be greatly appreciated.

:/

avatar image Digital-Phantom · Mar 28, 2015 at 01:55 PM 0
Share

Anybody ???

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lordlycastle · Mar 28, 2015 at 03:16 PM

First off Clamp sets the value of a variable between a range, but you are giving a zero range, i.e. min = max = 30. This could be a problem. Secondly never use GetCompoenent, or Find etc. methods in update, they are very resource consuming; set them in start to variable. Thirdly, don’t set the velocity of Rigidbody it is very easy to mess up there, because it’s the physics engines job. First try to just use AddForce, or MovePosition etc. Lastly, what you are doing is that you change the x value of the velocity, and then put the rigid body at new x position, 0, old z position so you are kind of applying torque, that is why it rotates. I don’t know what you actually are trying to achieve from this code exactly so can’t help you much. If you are trying to limit movement, then first calculate the new movement vector followed by a check to see it fits the criteria, and only then move the rigid body.

P.S. I don’t know if this is your real moment code, if so then you should put it in Update instead, because it calculations are going to waste every time FixedUpdate is called and frame isn’t really updated. That’s kinda a lot. I also hope you are using Time.deltaTime to smooth the movement.

Comment
Add comment · Show 2 · 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 Digital-Phantom · Mar 28, 2015 at 03:29 PM 0
Share

The code is working fine now, what I actually was trying to find out is why the revised code worked and the original code didn't. What was it about the changes that made it work correctly?

avatar image lordlycastle · Apr 05, 2015 at 08:00 PM 0
Share

@Digital Phantom Like I said above

...what you are doing is that you change the x value of the velocity, and then put the rigid body at new x position, 0, old z position so you are kind of applying torque, that is why it rotates…

In the new code you keep the y position so it doesn’t rotate.

P.S. If you add more information about question, use the Edit button. Not everyone reads all comments.

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

21 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

Related Questions

Calculate Vector3 for rotation or just change pivot; 0 Answers

Change player movement 0 Answers

Rigidody.velocity = Direction * speed; How to get direction? 1 Answer

Rigidbody.MovePosition() moves only in the GLOBAL z axis (forward and backward) 1 Answer

Can you pls help me fix this rigidbody collision problem 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