簡體   English   中英

三角函數 + 角度 - Object 圍繞玩家旋轉並面向鼠標方向

[英]Trigonometry + Angles - Object rotating around player and facing mouse direction

我不明白這段代碼是如何工作的,我正在尋求解釋。 此代碼在更新 function 中,不斷更新對象位置。

通過“面向鼠標方向”,我的意思是 object 就像地球繞太陽公轉一樣,但您可以選擇它當前在繞太陽旋轉線上的位置。

     public GameObject player;
     private Vector3 v3Pos;
     private float angle;
     private readonly float distance = 0.16f;
 
     private void Update()
     {
         v3Pos = Input.mousePosition;
         v3Pos.z = (player.transform.position.z - 
        Camera.main.transform.position.z);
         v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
         v3Pos -= player.transform.position;
         angle = Mathf.Atan2(v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
         if (angle < 0.0f) { angle += 360.0f; }
         transform.localEulerAngles = new Vector3(0, 0, angle);
 
         float xPos = Mathf.Cos(Mathf.Deg2Rad * angle) * distance;
         float yPos = Mathf.Sin(Mathf.Deg2Rad * angle) * distance;
         transform.localPosition = new Vector3(player.transform.position.x + 
         xPos * 4, player.transform.position.y + yPos * 4, 0);
     }

我在一段視頻中發現了這段代碼,它使 object(像槍一樣)圍繞播放器旋轉並同時跟隨鼠標,但我不明白它是如何工作的。 它是如何工作的? 另外,我不知道視頻在哪里,但如果有必要我會找到它。

它將附加的游戲對象從玩家的鼠標 cursor 的方向扭曲這么多單位(由於某種原因在本地),並且還轉向該方向的右側(由於某種原因在本地)。

以下評論中解釋了各個部分:

public GameObject player;
private Vector3 v3Pos;
private float angle;
private readonly float distance = 0.16f;

private void Update()
{
    // mouse position in screen space 
    // current value = (mouse x, mouse y, 0) )
    Vector3 v3Pos = Input.mousePosition;

    // sets the z coordinate to be the difference from the camera to the player
    //   along the forward world axis.
    // current value = (mouse x, mouse y, camera->player distance along forward)
    v3Pos.z = (player.transform.position.z - Camera.main.transform.position.z);

    // v3Pos now means the position of the cursor, projected onto plane the player
    // is on parallel to camera plane
    // This means it's a world space positioning of the mouse
    v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);

    // v3Pos now means the direction from the player to the mouse position 
    //   in world space.
    // Despite the name, now a direction, not a position!
    v3Pos -= player.transform.position;

    // finds the signed angle from right to the direction v3Pos represents.
    // in the range (-180, 180]. positive = counterclockwise
    angle = Mathf.Atan2(v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;

    // converts negative angle into an equivalent positive angle.
    if (angle < 0.0f) { angle += 360.0f; }

    // sets the forward axis angle of the transform this 
    //   MonoBehaviour is attached to as the same angle.
    // Since we measured from the right to the direction of the mouse, 
    //   this turns the right side to face the mouse.
    // This is done in local space for some reason, can't tell from code.
    transform.localEulerAngles = new Vector3(0, 0, angle);
 
    // finds x and y coordinates of a point in the same direction as 
    //   v3Pos the mouse from the player but at distance 
    // Could use v3Pos but with a z=0, normalized then * distance but 
    //   recalculating with trig works too.
    float xPos = Mathf.Cos(Mathf.Deg2Rad * angle) * distance;
    float yPos = Mathf.Sin(Mathf.Deg2Rad * angle) * distance;

    // sets the player's local position to be the position of the player 
    //   adjusted by the direction and distance of the point.
    // Also done in local space, can't tell why from code.
    // Weird to use something else's world position as the local position
    //   for something else.
    transform.localPosition = new Vector3(player.transform.position.x  
             + xPos * 4, player.transform.position.y + yPos * 4, 0);
 }

步驟是

// Get the mouse position on the screen
v3Pos = Input.mousePosition;
// Bring that point down until it is level with the player
v3Pos.z = (player.transform.position.z - Camera.main.transform.position.z);
// Find that point in world space coordinates
v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
// Find the vector from the player to that point
v3Pos -= player.transform.position;
// Calculate the angle between that vector and the X axis
angle = Mathf.Atan2(v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
// ensure the values are between 0 and 360
if (angle < 0.0f) { angle += 360.0f; }
// Set the item's rotation to that angle, so it faces the right direction
transform.localEulerAngles = new Vector3(0, 0, angle);
// Find the new position of the item on the orbit circle
float xPos = Mathf.Cos(Mathf.Deg2Rad * angle) * distance;
float yPos = Mathf.Sin(Mathf.Deg2Rad * angle) * distance;
// Set the item to it's new position
transform.localPosition = new Vector3(player.transform.position.x + xPos * 4, player.transform.position.y + yPos * 4, 0);

我不確定為什么最后一步中的坐標乘以 4,它們應該已經定位在半徑distance的圓上

暫無
暫無

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

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