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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

[SOLVED] Learning C# as I go

Discussion in 'Scripting' started by xStatusQuo, Apr 3, 2018.

  1. xStatusQuo

    xStatusQuo

    Joined:
    Apr 3, 2018
    Posts:
    7
    Hello, im trying to create my first game and its a pretty basic idea but im having trouble bringing that idea to life. I started out by making a very badly made sprite in photoshop and then tried to research ways to make him rotate 360 degrees by aiming where he looks at with the mouse. Well i succeeded in finding a small portion of code to get him to rotate BUT he isnt looking at the direct location of the cursor. Here is the code i have....




    void Update ()
    {
    var mouse = Input.mousePosition;
    var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
    var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
    var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0, 0, angle);
    }
     
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Is it turning in sync with your cursor movement, but in the wrong direction, or is it not turning?
    In the former case, you may need to adjust how you calculate and apply the angle based on your camera settings. What are your camera and sprite positions?
     
    xStatusQuo likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The code is working for me.
    Note: I began with "right direction as forward".
     
    xStatusQuo likes this.
  4. xStatusQuo

    xStatusQuo

    Joined:
    Apr 3, 2018
    Posts:
    7
    upload_2018-4-2_22-18-36.png

    Its not showing my cursor here but its location is almost directly to the right of the sprite and of course i want it to be in the direction of the nose of the sprite
     
  5. xStatusQuo

    xStatusQuo

    Joined:
    Apr 3, 2018
    Posts:
    7
    Its turning quite nicely, the problem, though, is the sprite isnt aiming at the direction the cursor is pointing to.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The easiest thing I can imagine is that you turn the sprite and reimport it so it's pointing to the right.
     
  7. xStatusQuo

    xStatusQuo

    Joined:
    Apr 3, 2018
    Posts:
    7
    Ok, i will try that and keep you updated
     
  8. xStatusQuo

    xStatusQuo

    Joined:
    Apr 3, 2018
    Posts:
    7
    I tried doing so and same thing happened. I tried rotating the sprite in the opposite direction it was moving when i would hit play and it would still go to the far left. I feel as though the code is directed toward the opposite side of the mouse but i cant figure out what is wrong
     
  9. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    To look in the direction of the cursor.......let me think...........................

    Is it possible to use transform.LookAt in 2D (just lock one of the axis?). You have too complex of code for me up there I'm terrible at math! D:

    Here's a topic just googled:

    https://answers.unity.com/questions/585035/lookat-2d-equivalent-.html

    Code (CSharp):
    1. Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    2.          diff.Normalize();
    3.          float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    4.          transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
     
    xStatusQuo likes this.
  10. xStatusQuo

    xStatusQuo

    Joined:
    Apr 3, 2018
    Posts:
    7
    You are a life saver! It works perfectly, thank you!
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya. That would work.. I was trying to avoid the change for ya. :)

    When you tested my idea, did you have a new sprite that was rotated outside of Unity?
    In 2D like that, right is forward.. so your character pointing up was technically pointing its right to your cursor earlier.. if that makes sense.

    Anyways, glad it's working for ya.
     
    astracat111 and xStatusQuo like this.
  12. xStatusQuo

    xStatusQuo

    Joined:
    Apr 3, 2018
    Posts:
    7
    Ahh, now its much more clear, im new everything including coding so any tips would be very much appreciated!
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, if you ever wanted to test that in practice, it would just mean you could create all of your sprites with the forward and not have to always adjust the turn (ie: -90).

    *Though, knowing how & that you can adjust them like that is also useful... lol
     
    xStatusQuo likes this.