簡體   English   中英

如何將玩家模型旋轉到鼠標當前光標位置 c# unity 3D?

[英]How to rotate player model to mouse current cursor position c# unity 3D?

我試圖讓我的角色指向我的鼠標光標所在的位置,但是這不能正常工作。 我已將我的腳本“test_1”附加到角色上並使其轉動,這很棒,但它需要面對我的鼠標位置。 當我在角色周圍移動鼠標時,它目前只是隨機旋轉。 我嘗試將腳本附加到主攝像頭,希望它能正常工作,但攝像頭只是不斷地轉一圈,因此腳本當前附加到玩家。 我只是在尋找一種方法來旋轉並將我的角色指向鼠標位置,同時用主攝像機(玩家視角)看一個角度。 這是視頻 一個很好的游戲參考是 Babo Violent 2。

我的代碼:

using UnityEngine;

public class test_1 : MonoBehaviour
{
    void Update()
    {
        Vector3 vecWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);

        float fAngle = AngleBetweenPoints(transform.position, vecWorldPos);

        transform.rotation = Quaternion.Euler(new Vector3(0f, fAngle, 0f));
    }

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

提前致謝!

嘗試添加 -90 度或 90 度偏移,也許這可能是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM