繁体   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