簡體   English   中英

你如何獲得玩家游戲 object 旋轉與球體游戲 object

[英]How do you get the player game object from rotating with the sphere game object

我目前正在制作一個 Katamari/Billy Hatcher 游戲,玩家必須在其中滾動球體。 當游戲開始時,玩家擁有正常的平台游戲控制,直到它接近一個球體,如果玩家按下“附加”按鈕,玩家將成為該球體的子級。 我遇到的問題是每當發生這種情況時,玩家都會隨着球體旋轉。 我嘗試凍結玩家的剛體,使其停止旋轉,但這只會停止球體的旋轉。 有什么方法可以在保持球體旋轉的同時停止播放器的旋轉?

圖片:在此處輸入圖片描述這是我的流程腳本:

Rigidbody hitRB;
public Vector3 offset = new Vector3(0, 0, 1);
public LayerMask pickupMask;
bool isAttached;


private void TouchingGum()
{
    RaycastHit hit = new RaycastHit();
    foreach (GameObject gumball in gumBalls)
    {
        
        if (Input.GetButtonDown("Attach") && Physics.Raycast(transform.position,transform.forward, out hit, attachRequireDistance, pickupMask))
        {
            isAttached = true;
            Debug.Log(true);
        }
        else
        {
            isAttached = false;
        }
    }


    if (isAttached)
    {
        hitRB = hit.collider.gameObject.GetComponent<Rigidbody>();
        Vector3 heldOffset = transform.right * offset.x + transform.up * offset.y + transform.forward * offset.z;
        hitRB.isKinematic = true;
        hitRB.MovePosition(player.transform.position + heldOffset);
    }
    else if(!isAttached && !hitRB == null)
    {
        hitRB.isKinematic = false;
    }

如果可以,請不要在這些情況下使用父/子關系。 我覺得總有更好的方法。 實現這一點的一種方法是獲取玩家的變換,並將正向添加到偏移量:

Vector3 offset = new Vector3(0, 0, 1);
LayerMask pickupMask; //change to mask of objects that can be picked up in editor.
public bool held;

void Update()
{
   RaycastHit hit;
   if (Input.GetKey(KeyCode.Mouse0) && Physics.Raycast(transform.position, transform.forward, out hit, 2, pickupMask))
   {
       held = true;
   }
   else
   {
       held = false
   }
   Rigidbody hitRB = hit.collider.gameObject.GetComponent<Rigidbody>();
   if (held)
   {
       Vector3 heldOffset = (transform.right * offset.x) + (transform.up * offset.y) + (transform.forward * offset.z);
       // if it still glitches, remove the previous line and add the line after this one.
       Vector3 heldOffset = transform.forward * offset.z;
       hitRB.isKinematic = true;
       hit.collider.gameObject.transform.position = transform.position + heldOffset;
   }
   if else (!held && !hitRB == null)
   {
      hitRB.isKinematic = false;
   }
}

此腳本使用光線投射和輸入來檢測玩家是否單擊鼠標左鍵並在 2 距離內查看 object 並帶有特定圖層蒙版。 然后它將速度設置為偏移量加上玩家的 position。 換句話說,這將獲得您在按下左鍵時查看的 object,並將其保持在您面前(或任何偏移量)。

暫無
暫無

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

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