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.

1 Answer

1

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.

Try the suggestion below. For bullet projectiles, just use hitscan and create a fake bullet effect either with a particle system or line renderer etc. ---------- For larger projectiles, such as rocket launchers or grenades (or if you also want bullets with colliders), you can do a sweep test (as suggested below), and before you move the projectile forward, raycast a certain distance forward, and if it hits something, teleport it to the hit pos, as to ensure that it doesn't move through walls (if it is really fast).