- Home /
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)
???
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.
:/
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.
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?
@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
Follow this Question
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