# Problem with Mathf.Clamp and Mouse Follow Script

I am attempting to have my character’s arm follow the player’s mouse. I have two problems.

First is I cannot figure out how to lessen the distance the player’s mouse must be from the character’s arm for the character’s arm to copy its movement. That is not the most clearly worded description so I’ve attached a link to a video that demonstrates the problem.

Second is that I also have added a Math.Clamp min and max so the player cannot windmill the character’s arm but I have not figured out how to stop the arm from snapping to the max rotation when it approaches the min rotation. instead of just stopping rotating and staying at “0” degrees it snaps to 90. Thanks in advance for your time.

Below I also have attached the mouse follow script and the script that defines the mouse the “Game Cursor” Script.

public static class GameCursor
{
public static Vector2 WorldPosition
{
get
{
float distance;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (_gamePlane.Raycast(ray, out distance))
return ray.GetPoint(distance);
// Ray did not hit plane
return Vector2.zero;
}
}
private static Plane _gamePlane = new Plane(Vector3.forward, 0);
}

``````using UnityEngine;

public class PointAtCursor : MonoBehaviour
{

public Vector3 targetPosition;
public float maxLength;
public GameObject ArmR;

void Start ()

{

}

private void Update()
{

if (Input.GetKey (KeyCode.E)) {

}

if (Input.GetKeyUp (KeyCode.E)) {

}

print (transform.eulerAngles.z);

Vector3 clampedRotation = transform.eulerAngles;
// Clamp the z value
clampedRotation.z = Mathf.Clamp(clampedRotation.z, -0 ,90);
// assign the clamped rotation
ArmR.transform.rotation = Quaternion.Euler(clampedRotation);

if(ArmR.transform.eulerAngles.z == 0){

Debug.Log ("AT0");

}

if(ArmR.transform.eulerAngles.z == 90){

Debug.Log ("AT90");

}

}

void Follow(){

ArmR.transform.up = -GameCursor.WorldPosition;
}

private void OnDrawGizmos()
{
Gizmos.DrawIcon(GameCursor.WorldPosition, "wallll", true);
}
}
``````

I believe the problem lies in your use of `transform.eulerAngles`. It always returns the rotation angles within the 0-360 degree range, so when your `z` rotation is < 0, it actually returns a `360 + z` value (e. g. if your `z` rotation is -5 degrees, `transform.eulerAngles.z` will show 355 degrees). Doesn’t your `print (transform.eulerAngles.z);` show that?

You could fix that by manually re-mapping the angle values.

``````Vector3 clampedRotation = transform.eulerAngles;
if (clampedRotation.z > 180.0f) clampedRotation.z = 360.0f - clampedRotation.z;
// Clamp the z value
clampedRotation.z = Mathf.Clamp(clampedRotation.z, -0 ,90);
``````