- Home /
stopping the gameObject if it is out of specified bounds.
I have a certain condition in which a gameObject can be moved only in x axis.I am using mouse x delta change as input (Input.GetAxis("Mouse X")) multiplied by some sensitivity. i have defined boundary value for x, so that it is clamped. the problem is that the gameObject moves some values greater than boundary value then resets back to boundary position. it is not stopping immediately at that point.
1) using physics calculations in Fixedupdate(). 2) moving using rigidbody.MovePosition() method.
Answer by kalen_08 · Jul 19, 2018 at 09:49 PM
Use Mathf.Clamp to achieve this right before using it for moving.
//Mathf.Clamp: clamps/confines the variable to the range you supply
x_move = Mathf.Clamp (x_move, MINIMUM_VALUE, MAX_VALUE);
//clamp it after you've done all calculations such as a multiplier ect.
// do whatever you want as far as moving the object.
Your answer
Follow this Question
Related Questions
How to cap this movement? 1 Answer
Move to mouse position 1 Answer
Clamping object movement in circle 1 Answer
Facing the players game object (upward, downward, left, right) on Android phone using joystick 0 Answers
Move object with finger swipe 0 Answers