【问题标题】:How do you get the player game object from rotating with the sphere game object你如何让玩家游戏对象随着球体游戏对象旋转
【发布时间】:2021-08-02 01:58:02
【问题描述】:

我目前正在制作一个 Katamari/Billy Hatcher 游戏,玩家必须在其中滚动球体。当游戏开始时,玩家拥有正常的平台游戏控制,直到它接近球体,如果玩家按下“附加”按钮,玩家将成为球体的子项。我遇到的问题是每当发生这种情况时,玩家都会随着球体旋转。我尝试冻结玩家的刚体,使其停止旋转,但这只会停止球体的旋转。有什么方法可以在保持球体旋转的同时停止播放器的旋转?

图片: enter image description here 这是我的流程脚本:

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;
    }

【问题讨论】:

  • 我只是制作一个脚本让玩家跟随位置而不是孩子。这样玩家和 Sphere 就有了各自独立的旋转。
  • 你也可以把它们都放在另一个空的游戏对象下

标签: c# visual-studio unity3d 3d rotation


【解决方案1】:

如果可以,请不要在这些情况下使用父/子关系。我觉得总有更好的方法。实现这一点的一种方法是获取玩家的变换,并将正向添加到偏移量:

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;
   }
}

此脚本使用 raycast 和 input 来检测玩家是否单击鼠标左键并查看 2 距离内的对象并带有特定的图层蒙版。然后它将速度设置为偏移量加上玩家的位置。换句话说,这会获取您在按下左键时查看的对象,并将其保持在您面前(或任何偏移量)。

【讨论】:

  • 您好尝试并修改了您的解决方案。但似乎玩家并没有将球放在他面前,我不知道这是否与处理他的偏移有关。 if(isAttached) { Vector3heldOffset = player.transform.right * offset.x + player.transform.up * offset.y + player.transform.forward * offset.z; hitRB = hit.collider.gameObject.GetComponent(); hitRB.velocity = player.transform.position +heldOffset; }
  • 如果您将offset 变量设置为(0, 0, 1),它应该将它放在播放器的前面。如果那不能将它放在玩家面前,请告诉我它是否完全握住了该物体。如果没有,那么也告诉我。并详细描述正在发生的事情。另外,我更改了代码。看看吧。
  • 对 AttachToGum 进行了更改。发生的唯一变化是每当我按下“附加”按钮时,它会根据 z 轴的偏移量不断移动球体。玩家对象仍然没有持有球体。我的代码有什么问题吗?
  • 它是否在不希望的位置出现故障?我仍然不明白发生了什么。能否请您用几句话描述您在运行游戏时看到的内容。
  • 好的,谢谢,已经测试过了,它可以工作了!!
猜你喜欢
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多