Поворот плеера немного выключен - PullRequest
0 голосов
/ 11 сентября 2018

Я новичок в unity / c # и мне нужна помощь с ротацией моего игрока. Пока я удерживаю курсор мыши на положительной оси Y (если середина плеера равна 0), игрок стоит немного выше курсора, а на отрицательной оси Y игрок оказывается чуть ниже курсора. Вот gif для демонстрации:
https://gyazo.com/e417962c20e186f3c6419c23bf8263f6

Вот мой код для вращения.

public class LookTowardMouse : MonoBehaviour 
{
    void Update()
    {
        //Get the Screen positions of the object
        Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);

        //Get the Screen position of the mouse
        Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);

        //Get the angle between the points
        float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);

        //Rotate player

        Debug.Log(angle);
        transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
    }

    float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
    {
        return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
    } 
}
...