- Home /
2D Rotate sprite (arm) so it faces the mouse.
Hello,
I found several answers to this question but they don't seem to work for me. I want to find one that actually works before I dig into the code and see how it is put together.
I have a sidescrolling game and i want to rotate the arm of my character so it points towards the mouse.
I tried several solutions and the best one was working while my character was facing right but the controls were inverted when facing left, i guessthis is because i simply flip the X of the sprite when it turns.
I started looking for a way using raycast since this might solve my problem?
I looked at http://www.unifycommunity.com/wiki/index.php?title=LookAtMouse and the script looks promising but nothing happens when I attach it to the object.
Any help with this would be appreciated, and if possible I would prefer a C# solution.
Answer by zientasek · May 27, 2014 at 09:17 PM
using UnityEngine;
using System.Collections;
public class rotateToMouse : MonoBehaviour {
// Update is called once per frame
void Update ()
{
Vector2 mouse = Camera.main.ScreenToViewportPoint(Input.mousePosition); //Mouse position
Vector3 objpos = Camera.main.WorldToViewportPoint (transform.position); //Object position on screen
Vector2 relobjpos = new Vector2(objpos.x - 0.5f,objpos.y - 0.5f); //Set coordinates relative to object
Vector2 relmousepos = new Vector2 (mouse.x - 0.5f,mouse.y - 0.5f) - relobjpos;
float angle = Vector2.Angle (Vector2.up, relmousepos); //Angle calculation
if (relmousepos.x > 0)
angle = 360-angle;
Quaternion quat = Quaternion.identity;
quat.eulerAngles = new Vector3(0,0,angle); //Changing angle
transform.rotation = quat;
}
}
May be not intuitive but it works.
Make sure that when arm is pointing upwards rotation is 0,0,0
Thanks, I think this is similar to something I found and tried earlier.
However it suffers from the same issues with the controls getting inverted when my character faces left, I am using the character movement script from the Unity assets pack and it simply flips the image when you turn around.
By inverted I mean that if I move the mouse up 45 degrees it will move down 45 degrees ins$$anonymous$$d.
I just got this working like I wanted, just added a if statement to invert the X-axis of relmousepos if the character is facing the other way.
cheers!
This works almost perfectly for me as well, thanks. The only thing that I notice is that the angles are a bit off at some points. For example at 0, 90, 180, 360 degrees, the object dead on points at the mouse. At 45, 135, 225, 315 degrees, it's very noticeably off. Perhaps it is because my game is 16:9 ins$$anonymous$$d of a square? I don't know exactly what is going on in your code yet, so I'm not sure. Any ideas? At 45 degrees, the object points above the mouse and at 135 degrees the object points below the mouse.
I'm glad I helped. I spend over an hour to figure out how to accomplish this. At the end, I noticed that my first attempt was best. And I have no idea why offset is present. I'll try to fix this later.
A little late to the party, but I am having this same issue. the one thing I need to accomplish is to get reverse the x-axis for the mouse when my character is flipped. Taxen0 I was wondering if you could share the line of code you used to do this?
Your answer
Follow this Question
Related Questions
raycast to determine pivot 1 Answer
2D animation : problem with rotation interpolation 1 Answer
2D Sprite Rotation 0 Answers
Pointing object at mouse using raycast only works when object is center screen. 2 Answers
Mathf.Clamp is 'Sticky'? 1 Answer