Search Unity

Object rotate towards mouse not working when I move player

Discussion in '2D' started by mykod, Jul 3, 2015.

  1. mykod

    mykod

    Joined:
    Jul 2, 2015
    Posts:
    3
    Hello. I'm new to unity and I posted a question like 15 hours ago and it wasn't even approved yet. So I just wanted to know, does this always take so long? Cause in other forums that I used before it wasn't even needed an approval.

    Either way, here's my question, if anyone could help that would be great :)


    "Hello. I'm beggining to learn Unity and I'm having a problem. I followed a tutorial on how to point an object towards a mouse, and it works, but when I climb something (my player y position increases) it does not work.

    I uploaded a video so you can see it by yourselves as it is hard to actually explain:


    And here's my code:

    Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition - transform.position);
    difference.Normalize ();
    float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

    transform.rotation = Quaternion.Euler(0f,0f,rotationZ);

    Thank you very much."
     
  2. mykod

    mykod

    Joined:
    Jul 2, 2015
    Posts:
    3
    Found out the problem.

    I needed to change the
    Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition - transform.position);
    to:
    Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;

    because I only want to convert the input.mousePosition to ScreenToWorldPoint :)


    Still, how long does it take a question to be approved? Mine was there about 15 hours...