I’m trying to make a simple 2D-shooting game (top down) and now I want my character to rotate towards the position of the mouse (360 degrees). I’ve been searching around for hours on Google and found the same problem multiple times but none of the codes seemed to work for me. The code that’s the closest is the following:
I created a new 2D project. I imported a Spirte (that was just an arrow pointing up). I copied your script and added it to my sprite. My arrow spun around all 360 degrees following my mouse.
Now since the 0 degree point of Atan2 is pointing directly along the X-axis i had to add this line:
angle-=90;
So my arrow would actually point directly at the mouse (since i drew it pointing up to start with). but that was irrelevant to the actual spinning full 360. Even without that line your code spun my sprite through the full 360 degrees.
Try duplicating what I did. Make a new 2D project with just single sprite and your script attached to it. You should see it spin 360. If it does you know something else is going on in your project where it isn’t working.
One thought… is your sprite symmetrical along the x-axis. That might make it seem like its not spinning when it is.
I tried to make a simple cube sprite and it still doesn’t work. Do you mind attaching your arrow sprite so I can try that one? The sprite I am using is not symmetric along the x-axis, so that might be the problem but it’s still not working with my cube.
Where should I put the code where angle is put to 90? Since angle is calculated in the script, why should I fix it to a certain degree?
I just opened MSPaint… drew a simple arrow and saved it as a jpg. Then imported into Unity and set it as the image in a generic 2D sprite Also attached your script to the same GO
I’m sorry, I still don’t get it to work… I’ve uploaded my files and the scene can be opened under assets. It’s called “cubetest”. I don’t get what I’m doing wrong. It should rotate all the way around 360 degrees (the red part is my “pointer” for the cube).
I just downloaded and tested the project - the script runs flawlessly here - the red line exactly points towards the mouse position at all the places on screen - no console errors either. The only small thing I found is that I have Unity 5.5.0f3 so I opened in that and you seem to have used Unity 5.5.1. Although highly unlikely, but perhaps something to do with that ?
Just downloaded the project as well with 5.5.1f and it worked flawlessly. What exactly is it doing when you run it? I notice you have your scene view on top of your Game view. If you try to move the mouse around the cube in the SCENE view (which is on top)… it will look like its only doing 180 degrees but the cube that is pointing to your mouse is way down below in the game view
Make sure you rotating it around the cube on the bottom in the game view.