- Home /
How to lock rotation in a specific way?
I'm trying to make a simple top down game, and at the moment the player moves with aswd, etc, and I want the player to always face the mouse. for example, I have [^] <-- sprite The carrot should face the mouse always. My script so far is thus:
void TurnBody()
{
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
transform.LookAt(Camera.main.ScreenToWorldPoint(mousePos), Vector3.forward);
//transform.rotation = Quaternion.Euler(0, 0, -(transform.rotation.eulerAngles.z));
//rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
//transform.eulerAngles=new Vector3(0,0,transform.eulerAngles.z);
}
I've tried using atan2, various quaternion stuff, which did not work. The uncommented works great, except for some reason the sprite is not "flat" unless the mouse is very close to the sprite. The commented is some stuff I tried in order to lock the rotation on the various axes, but when I do, the rotation is no longer smooth and becomes jerky, and often goes in the wrong direction. (opposite to the mouse) I'll keep trying things, though any constructive input would be appreciated, thanks. I am also open to entire other solutions to the problem.
Answer by Maurice_B · Jul 17, 2016 at 08:28 AM
Just To check : is the Z of your player 0. It might be better to use :
void TurnBody() { Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y,transform.position.z); transform.LookAt(Camera.main.ScreenToWorldPoint(mousePos), Vector3.forward); }
Therefore it would always be looking at something of the same depth.
Your answer
Follow this Question
Related Questions
object rotates toward mouse? 2D Top Gameplay 6 Answers
Why Rotate player toward mouse direction keeps increasing rotation speed? 1 Answer
Following the mouse Cursor does not work correctly after Flip() Function 0 Answers
Mouse cursor position shared beetwen different builds. 3 Answers
2D arm rotation using mouse 0 Answers