- Home /
I need help with clamping a rotation towards the mouse position
Hello guys! I'm working on a small 2D-shooter and need help with clamping the rotation of the characters weapon. The Rotation system works perfectly, however when I want to clamp the rotation, it only works when the character is faced towards the right side and behaves weirldy when the character is faced towards the left.
How could I fix this?
Example without clamping:
Example with clamping:
The code used for rotating:
public Vector3 pos;
Transform player;
public int maxAngle;
public int minAngle;
public int Angle { get; private set; }
void Start()
{
player = transform.parent;
Angle = 0;
}
public void Update()
{
pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = pos - transform.position;
dir.Normalize();
Angle = Mathf.RoundToInt(Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
//Used to Clamp the Angle, not working properly :(
Angle = Mathf.Clamp(Angle, minAngle, maxAngle);
if (player.localScale.x == 1)
{
transform.localRotation = Quaternion.Euler(0f, 0f, Angle);
}
else
{
transform.localRotation = Quaternion.Euler(0f, 0f, -Angle -180f);
}
}
It is maybe cause you are scaling player.x? i would suggest using SpriteRender.flip rather than scaling and test the same code
Answer by haruna9x · Jan 17, 2019 at 11:27 AM
public class ArmRotater : MonoBehaviour
{
public Vector3 pos;
public Vector2 dir;
public Transform player;
public int maxAngle;
public int minAngle;
public int angle;
void Start()
{
player = transform.parent;
angle = 0;
}
public void Update()
{
pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
dir = pos - transform.position;
dir.Normalize();
if (player.localScale.x == -1)
{
dir.x *= -1;
}
angle = Mathf.RoundToInt(Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
angle = Mathf.Clamp(angle, minAngle, maxAngle);
transform.localRotation = Quaternion.Euler(0f, 0f, angle);
}
}
Yes that works just as intended! Thank you very much! :D
Answer by TheLuigiplayer · Jan 16, 2019 at 11:47 PM
I got to fix it partially. It still behaves weirldy when the character is faced towards left. When the weapon is lowered to the minimum angle it jumps to the maxium angle:
The Code now looks like that:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArmRotater : MonoBehaviour
{
public Vector3 pos;
Transform player;
public int maxAngle;
public int minAngle;
public int Angle { get; private set; }
void Start()
{
player = transform.parent;
Angle = 0;
}
public void Update()
{
pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = pos - transform.position;
dir.Normalize();
Angle = Mathf.RoundToInt(Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
if (player.localScale.x == 1)
{
Angle = Mathf.Clamp(Angle, minAngle, maxAngle);
transform.localRotation = Quaternion.Euler(0f, 0f, Angle);
}
else
{
Angle = Mathf.Clamp(Angle, 180-maxAngle, 180-minAngle);
transform.localRotation = Quaternion.Euler(0f, 0f, -Angle - 180f);
}
}
}
Just curious: why are pos and Angel public? Are other scripts accessing them?
Cool. What $$anonymous$$ and max values do you set in the inspector?