C
C#2y ago
henke

✅ C# unity rotation clamping.

So I want it to be able to rotate in a 180 degree angle from the left side and 180degree from the right side.. depending on where my mouse is at and which direction i'm facing. Basically limiting the angles the player can have depending on which side hes facing. The right side (changeDirection.x == 1) works. The left side (changeDireciton.x == -1) doesn't work. Any help or code rewrite suggestion appreciated !
// private void Update()
{

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0f;

Vector3 aimDirection = (mousePos - transform.position).normalized;


if(changeDirection.x == -1)
{
angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 180f);
angle = Mathf.Clamp(angle, -90f, 90f);
print(angle);
}
else if(changeDirection.x == 1)
{
angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg);
angle = Mathf.Clamp(angle, -90f, 90f);
//print(Mathf.Rad2Deg);
}

zAngle = new Vector3(0, 0, angle);
transform.eulerAngles = zAngle;
}
// private void Update()
{

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0f;

Vector3 aimDirection = (mousePos - transform.position).normalized;


if(changeDirection.x == -1)
{
angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 180f);
angle = Mathf.Clamp(angle, -90f, 90f);
print(angle);
}
else if(changeDirection.x == 1)
{
angle = (Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg);
angle = Mathf.Clamp(angle, -90f, 90f);
//print(Mathf.Rad2Deg);
}

zAngle = new Vector3(0, 0, angle);
transform.eulerAngles = zAngle;
}
4 Replies
henke
henkeOP2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Omnissiah
Omnissiah2y ago
lol did you just $ code yourself
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server