I have a problem with mathf.clamp…how to use it to move left and right??
I have my player using character controller or rigidbody…
if(Input.getbuttondown(keycode.Leftarrow)) { movedirection.x=speed*Time.deltatime;
// in there my player at x=0:now i have to move my player at x=1 when once left arrow is pressed…if again left arrow is pressed +1:someone help with this // }
You wouldn't use $$anonymous$$athf.Clamp() itself to move left and right.
Clamp() keeps takes a number, an upper bound, and a lower bound. It returns either:
The number
The lower bound (if your number was below this)
The upper bound (if your number was above this)
You would use Clamp() to prevent a value from being too large or small prior to assigning to as some position component.
Answer by Robert_s_a · Mar 10, 2017 at 09:40 AM
As TreyH said, MathF.Clamp() would be used to limit the motion so it doesn't go beyond a set range. You may still need this if you need to limit the player to specific boundaries. Personally, I use Input.GetAxis("Horizontal") for movement, it's a smoother result than with getbuttondown, unless you specifically want it to jump one step at a time. Here's an example.
public gameObject MyObj;
float moveHorizontal;
float maxLeft = -15.0f;
float maxRight = 15.0f;
float moveSpeed = 0.5f;
void Update()
{
// Read the keyboard input ( - left and + right arrows or - A and + D keys)
moveHorizontal = Input.GetAxis("Horizontal");
// adjust the input for your scale and clamp it to your boundaries
moveHorizontal = Mathf.Clamp(moveHorizontal*moveSpeed*Time.deltatime,maxLeft,maxRight);
// apply the motion to your object
MyObj.transform.position = MyObj.transform.position+new Vector3(MoveHorizontal,0,0);
}
You can use GetButtonDown with leftarrow and also with rightarrow in place of GetAxis if you'd like. GetAxis does handle both directions with one command, however.
You can do the same thing with Input.GetAxis("Vertical") for up and down if you want, and that would go in place of the second 0 in the Vector3.
Or if you're doing 2 dimensions, you'd probably be using a Vector2(x , y) without the third 0 for z.
Hope this helps.
This will clamp your movement vector, but not the position itself. The frame-wise increment will be bound (the product of moveSpeed, moveHorizontal, and the time delta), but the object being moved is not limited beyond that in this setup.
Thanks for the correction, TreyH. It was a bit late last night when I posted this, and yeah, I clamped it in the wrong place. One of the hazards of insomniac program$$anonymous$$g, I guess. :P
Answer by Nivasraj · Mar 10, 2017 at 06:55 AM
Thanks for that…but still i cant understand how to use mathf.clamp with my player…
i used mathf.clamp with my player…but the code not working…can you give me some code !!……………thanks
I made a mistake in the code above, and used $$anonymous$$athf.Clamp on the calculation for the next step in the movement, not as a limit to the entire range of movement itself. (Thanks TreyH for pointing me in the right direction :)
Take the $$anonymous$$athf.Clamp part out of the line its on, so it becomes: moveHorizontal = moveHorizontal*moveSpeed*Time.deltatime;
and change the line below (which actually moves the player) to: $$anonymous$$yObj.transform.position = new Vector3($$anonymous$$athf.Clamp(($$anonymous$$yObj.transform.position.x+moveHorizontal),maxLeft,maxRight)),0,0);
This puts the clamp on the overall range of motion in the X direction, rather than just on the current step.
Basically $$anonymous$$athf.clamp has nothing to do with movement itself and, $$anonymous$$athf.Clamp(someNumber,$$anonymous$$Value,maxValue); is the same code as:
if (someNumber < $$anonymous$$Vaue) { someNumber = $$anonymous$$Value; } else if (someNumber > maxValue) { someNumber = maxValue; }
Hope this is helping. :)
Your answer
Follow this Question
Related Questions
Problem with mathf.clamp on rigid body without gravity 1 Answer
Error when importing mathematics 2 Answers
Min and Max value? 1 Answer