- Home /
Question by
RavenUnchained · Sep 10, 2017 at 06:51 PM ·
rotationobjectcursorfollowing
How to rotate an object according to the cursor position ? ( C# )
Here is my problem, i have a triangle in the middle on my screen, i want it to rotate following my cursor with the top of the triangle. I got my cursor position in a World Scale with ScreenToWorldPoint. Here is the script I wrote.
void Update () {
v3 = Input.mousePosition;
v3.z = -10;
v3 = Camera.main.ScreenToWorldPoint(v3);
cursor_x = v3.x;
cursor_y = v3.y;
if (cursor_x < 0)
{
position_x = -1;
}else
{
position_x = 1;
}
if (cursor_y < 0)
{
position_y = -1;
}else
{
position_y = 1;
}
if (position_x == -1 && position_y==-1)
{
tan = cursor_y / cursor_x;
angle =(Mathf.Atan(tan) * Mathf.Rad2Deg);
this.GetComponent<Rigidbody2D>().transform.Rotate(0f, 0f, -90 - angle, Space.World); //= Quaternion.Euler(0f, 0f, -90-angle);
}
else if(position_x==1 && position_y==-1)
{
tan = cursor_y / cursor_x;
angle = (Mathf.Atan(tan) * Mathf.Rad2Deg);
this.GetComponent<Rigidbody2D>().transform.Rotate(0f, 0f, 90 + angle, Space.World); //= Quaternion.Euler(0f, 0f, 90+angle);
}
else if (position_x == -1 && position_y == 1)
{
tan = cursor_x / cursor_y;
angle = (Mathf.Atan(tan) * Mathf.Rad2Deg);
this.GetComponent<Rigidbody2D>().transform.Rotate(0f, 0f, - angle, Space.World); //= Quaternion.Euler(0f, 0f, -angle);
}
else if (position_x == 1 && position_y == 1)
{
tan = cursor_x / cursor_y;
angle = (Mathf.Atan(tan) * Mathf.Rad2Deg);
this.GetComponent<Rigidbody2D>().transform.Rotate(0f, 0f, angle, Space.World); //= Quaternion.Euler(0f, 0f, angle);
}
Comment
Your answer
Follow this Question
Related Questions
rotation of the object to a given angle 1 Answer
Make object appear within camera view 0 Answers
How to make an enemy that walks around a platform? 0 Answers
Arm Rotation toward Cursor Script Help 1 Answer
Rotate to mouse cursor error 1 Answer