- Home /
How would I lock rotation in unity's new input sytem?
I am rotating an object towards the mouse or controller using unity's new input system, using this script.
public class HandController : MonoBehaviour
{
public void ReadMouseInput(InputAction.CallbackContext context)
{
Vector2 mousePosition = context.ReadValue<Vector2>();
Vector2 objectPosition = (Vector2)Camera.main.WorldToScreenPoint(transform.position);
Vector2 direction = (mousePosition - objectPosition).normalized;
RotateAim(direction);
}
public void ReadStickInput(InputAction.CallbackContext context)
{
Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
RotateAim(stickDirection);
}
public void RotateAim(Vector2 direction)
{
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
}
The rotation works well, however, I would like to be able to lock the rotation by setting a range between two values in which the object can rotate. I have been trying to find ways to do this, but pretty much any way of locking rotation I've found uses the old input system in some way. Is there a way I could implement this into my script using new input system? and if so, how would I go about doing that? thanks.
Answer by AaronBacon · Dec 06, 2021 at 05:00 PM
All you need to do is use Mathf.Clamp as you're setting the rotation
For example:
⠀
public void RotateAim(Vector2 direction)
{
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Clamp(angle,-75,75)));
}
⠀
This should lock your rotation between -75 and 75 degrees for example
hm...
Assets\Scripts\HandController.cs(29,71): error CS1501: No overload for method 'Clamp' takes 2 arguments
public class HandController : MonoBehaviour
{
public void ReadMouseInput(InputAction.CallbackContext context)
{
Vector2 mousePosition = context.ReadValue<Vector2>();
Vector2 objectPosition = (Vector2)Camera.main.WorldToScreenPoint(transform.position);
Vector2 direction = (mousePosition - objectPosition).normalized;
RotateAim(direction);
}
public void ReadStickInput(InputAction.CallbackContext context)
{
Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
RotateAim(stickDirection);
}
public void RotateAim(Vector2 direction)
{
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Clamp(-75, 75)));
}
}
this is the script currently
Sorry, that's my fault, Mathf.Clamp(-75,75) should be Mathf.Clamp(angle,75,-75)
I've corrected the above code now, should work
it's working! although, now I have another error. The object being rotated is a pair of hands that is a child to the player, with a gun as a child to the hands. The error I am having is that now that the rotation is locked, the gun isn't flipping with the player properly. do you know how I could fix this? I am flipping the player using:
public void Flip()
{
if ((inputX < 0 && facingRight) || (inputX > 0 && !facingRight))
{
facingRight = !facingRight;
transform.Rotate(new Vector3(0, 180, 0));
}
}
Ah, so I think the problem is in the HandController script the same line that locks the rotation also sets its y rotation to 0. Try modifying that RotateAim function like so
⠀
public void RotateAim(Vector2 direction)
{
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(transform.position.x, transform.position.y, Mathf.Clamp(-75, 75)));
}
⠀
That should make it leave the other rotation values unchanged
nope, even with that changed it's still being wonk. If I leave my hand off of the things I use to aim, it will flip with the character. But as soon as I touch the right knob of a controller or the mouse, it snaps back to being flipped the other way
Your answer
![](https://koobas.hobune.stream/wayback/20220613050237im_/https://answers.unity.com/themes/thub/images/avi.jpg)