- Home /
Rotation along 8 points following mouse.
Okay, so I've been searching for a while on this and no one has ever asked this before to my understanding.
I'm trying to make a 2D player rotate based on the mouse. I figured that out fine, but then I wanted that sort of thing to work for 8 different sprites.
I figured I could have an invisible rotating object that follows the mouse that tells me where the mouse is rotated, and then to just send out an int based on whether that rotation falls between some predetermined degrees.
But for some reason it's simply not working, it's returning nothing at all. The rotZone int stays at 0.
My best guess is there's a problem somewhere with how I'm getting its rotation. (But there really might be some other issue as well.)
I tried:
transform.rotation.z
this.transform.rotation.z
this.transform.rotation.eulerAngles.z
private void Update ()
{
Vector3 difference = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + Rotoffset);
findRotationzone();
}
public int rotZone;
private void findRotationzone()
{
if (this.transform.rotation.eulerAngles.z >= -22.5 && transform.rotation.eulerAngles.z >= 22.5 && rotZone != 1)
{
rotZone = 1;
Debug.Log("N");
}
if (this.transform.rotation.eulerAngles.z >= 22.5 && transform.rotation.eulerAngles.z >= 45.5 && rotZone != 2)
{
rotZone = 2;
Debug.Log("NE");
}
//Happens again to add up to 8 times.
Answer by Ymrasu · Jan 26, 2019 at 03:13 AM
Your code for following the mouse is fine. It's checking if you are between rotations that you are having trouble with. When checking your eulerAngle, it returns a number between 0 and 360 going counter-clockwise.
Instead of checking between angles I would just chain else if in increasing rotation checks. If the first one is true it doesn't bother checking the rest. But doing it like that means that checking if rotZone is already assigned to it wont work, it's not a problem to assign to rotZone every frame.
void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ - 90);
findRotationzone();
}
public int rotZone;
private void findRotationzone()
{
float r = (transform.localEulerAngles.z + 22.5f);
if (r < 45) {
rotZone = 0;
Debug.Log("N");
} else if (r < 90) {
rotZone = 1;
Debug.Log("NW");
} else if (r < 135) {
rotZone = 2;
Debug.Log("W");
} else if (r < 180) {
rotZone = 3;
Debug.Log("SW");
} else if (r < 225) {
rotZone = 4;
Debug.Log("S");
} else if (r < 270) {
rotZone = 5;
Debug.Log("SE");
} else if (r < 315) {
rotZone = 6;
Debug.Log("E");
} else if (r < 360) {
rotZone = 7;
Debug.Log("NE");
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
float length = 10f;
var startPoint = transform.position;
float angle1 = ((rotZone * 45) + 90 + 22.5f) * Mathf.Deg2Rad;
float angle2 = ((rotZone * 45) + 90 - 22.5f) * Mathf.Deg2Rad;
var endPoint1 = new Vector2(startPoint.x + length * Mathf.Cos(angle1), startPoint.y + length * Mathf.Sin(angle1));
var endPoint2 = new Vector2(startPoint.x + length * Mathf.Cos(angle2), startPoint.y + length * Mathf.Sin(angle2));
Gizmos.DrawLine(startPoint, endPoint1);
Gizmos.DrawLine(startPoint, endPoint2);
}
I assigned the object's z rotation to a variable to make it easier to work with, then I added 22.5 so I don't have to worry about wrapping between 360 and 0. I also added a OnDrawGizmos to help visualize the zones quickly. And remember this script is attached to your player object.
Thank you so much! Thanks especially for adding in the Gizmo, that was a wonderful addition. Everything works wonderfully.
Your answer
Follow this Question
Related Questions
"If" Statement 1 Answer
Distance sensor 1 Answer
why != is different from var1 = !var2? 2 Answers
Check Material C# 1 Answer