My object keeps turning when I move my mouse, how do I fix this?
I have been pretty new to programming in Unity. I want my object/sprite to look at the position of my mouse so I found some code:
using UnityEngine;
using System.Collections;
public class PlayerController_02 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//position of the mouse = x-value and y-value and z-value
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
//get the position of the mouse on the screen
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
//the angle == tan(z/x) * radiant to degree
float angle = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
//rotate the GameObject (transform.rotation, axis around the point to turn)
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
// transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Debug.Log(transform.rotation);
}
}
I am using this for a 2D game. But the problem is, is that when I move my mouse over 90 degrees on the screen relative to my object, the player will keep turning in one direction very slowly, when I increase or decrease the degrees relative to the 90, the object will start turning faster and faster. I tried to find a solution and I know that the object must stop moving when it has aligned with the mouse position but I have no idea how to fix this.
Sorry for the English grammer if it's bad, but I tried my best :P I would appreciate it to get some help!
BTW the "// text" is what I wrote myself to help me understand what is going on. If this is wrong I would appreciate it if you could correct it as well.
Answer by Potatobob · Aug 28, 2016 at 03:41 PM
I found this answer for you by googling , "transform.lookat 2d".
Many topics such as this one appeared and were answered in the past. I did use many of them myself.
void Update()
{
Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
}
this code is probably what you want in your Update function. It is shorter , cleaner and work perfectly with 2D.
The only problem you might encounter with this is that your object might be pointing in the wrong direction (example he could have 90 degrees of offset with what you want.)
This can also be solved by changing the last part of the last line I shown you.
transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90); <------- the "-90" can be replaced by another number or simply removed.
I hope my english is not too bad and you will be able to use this to fix your issue.
Finally here is the link of the answer I looked up for you:http://answers.unity3d.com/questions/585035/lookat-2d-equivalent-.html
Thank you for your answer, and your English is good enough so I understand :P
But It hasn't fixed the problem for me, I just tried it out but it basically does the same thing :/
Your answer
![](https://koobas.hobune.stream/wayback/20220612091724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing a Transform's rotation to match a Vector using Quaternions 0 Answers
Order of the rotations around the axes with Euler angles 1 Answer
Quaternion (look)rotation issue (gimbal lock?) 0 Answers
Rotate clockwise/counterclockwise using Quaternion.Slerp() 0 Answers
strange rotation in 2d 0 Answers