Clamping mouse movement when dragging Player object

Hi,

I have a fixed camera facing along the Z-axis and the Player ahead of it. I am using the Unity ‘DragRigidbody’ script to drag the Player in the X and Y direction (Z is locked). Now, I would like to be able to restrict the amount that a user is able to drag the Player along these axes. The reason for this is the collision detection I have implemented doesn’t seem to be reliable when moving the Player quickly/repeatedly against the surrounding walls. The Player still partially/fully moves through them. I am using the ‘DontGoThroughThings’ script for this.

If anyone can recommend a reliable method of avoiding this issue, it would be greatly appreciated.

I haven’t used this one, but i know that there are different collision checking methods.
If you look at the inspector window, there is “Collision Detection” which allows you to chose whether collision checking would be discrete/Continuous/Dynamic Continuous. As far as I remember, Continuous method requires more calculation, i think it uses ray to predict if it is going to hit something.

You can look at it at unity site
http://unity3d.com/support/documentation/Components/class-Rigidbody.html

Great - thanks for the suggestion PforPepsi.

I have instead decided to clamp the movement as it is more reliable. I will be trying out the discrete/Continuous method very soon, though :slight_smile:

For other people looking to do something similar, this is what I used:

Note, this must be called from an Update() function as it needs to check/assign the X and Y position of the Player every frame. minX, maxX, minY, and maxY are public float variables at the top of the script so that their values can be tweaked whilst in the Inspector. Simply attach the script to the object you want to clamp the movement of.

    // If Player's X exceeds minX or maxX..
    if (transform.position.x <= minX || transform.position.x >= maxX)
    {
        // Create values between this range (minX to maxX) and store in xPos
        float xPos = Mathf.Clamp(transform.position.x, minX + 0.1f, maxX);

        // Assigns these values to the Transform.position component of the Player
        transform.position = new Vector3(xPos, transform.position.y, 
                                               transform.position.z); 
    }

    // If Player's Y exceeds minY or maxY..
    if (transform.position.y <= minY || transform.position.y >= maxY)
    {
        // Create values between this range (minY to maxY) and store in yPos
        float yPos = Mathf.Clamp(transform.position.y, minY, maxY);
         
        // Assigns these values to the Transform.position component of the Player
        transform.position = new Vector3(transform.position.x, yPos, 
                                         transform.position.z);
    }

Thanks for this. Here’s a small edit with the clamp margins:

// If Player's X exceeds minX or maxX
if(transform.position.x <= minX || transform.position.x >= maxX){
	
	// Create values between this range (minX to maxX) and store in xPos
	float xPos = Mathf.Clamp(transform.position.x, minX + 0.1f, maxX - 0.1f);
	
	// Assigns these values to the Transform.position component of the Player
	transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
}

// If Player's Z exceeds minZ or maxZ
if(transform.position.z <= minZ || transform.position.z >= maxZ){
	float zPos = Mathf.Clamp(transform.position.z, minZ + 0.1f, maxZ - 0.1f);
	transform.position = new Vector3(transform.position.x, transform.position.y, zPos);
}