Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2d character rotation towards mouse

Discussion in 'Scripting' started by Bogaland, Feb 19, 2017.

  1. Bogaland

    Bogaland

    Joined:
    Feb 19, 2017
    Posts:
    32
    Hello

    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class rotateToMouse : MonoBehaviour
    6. {
    7.     void Update()
    8.     {
    9.         //rotation
    10.         Vector3 mousePos = Input.mousePosition;
    11.         mousePos.z = 0;
    12.  
    13.         Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
    14.         mousePos.x = mousePos.x - objectPos.x;
    15.         mousePos.y = mousePos.y - objectPos.y;
    16.  
    17.         float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    18.         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    19.     }
    20. }
    This code is simply attached to my player object. The code works fine from 0 to 180 degrees but does not work for the entire 360. Any ideas?
     
    amedkurd and FwuffFox like this.
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    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:
    Code (CSharp):
    1. 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.
     
  3. Bogaland

    Bogaland

    Joined:
    Feb 19, 2017
    Posts:
    32
    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?
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    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

    And I put the angle here:
    Code (CSharp):
    1.  float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    2. angle-=90;
    3.         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    But only because I drew the arrow up.. IF you draw the arrow facing right you won't even have to do that.
     
  5. Bogaland

    Bogaland

    Joined:
    Feb 19, 2017
    Posts:
    32
    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).

    http://s000.tinyupload.com/index.php?file_id=08800428740114465831
     
  6. shashank1981

    shashank1981

    Joined:
    Feb 20, 2017
    Posts:
    24
    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 ?
     
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    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.
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
  9. Bogaland

    Bogaland

    Joined:
    Feb 19, 2017
    Posts:
    32
    Gah... I was rotating it in the scene window :mad: It works fine in the game window! Thanks haha!
     
  10. inewland

    inewland

    Joined:
    Dec 6, 2012
    Posts:
    20
    A slight modification to what is above...

    Code (CSharp):
    1. Vector3 mousePos = targetCamera.ScreenToWorldPoint(Input.mousePosition);
    2. Vector3 perpendicular = transform.position - mousePos;
    3. transform.rotation = Quaternion.LookRotation(Vector3.forward, perpendicular);
     
    iLamento likes this.