Unity 3D-碰撞

问题描述 投票:0回答:1

我对物理学有疑问。这是我第一次使用3D,所以可能只是一个初学者的错误。

我只是想创建一个简单的播放器控制器,并使其无法通过槽形立方体。问题在于,直接进入多维数据集时,播放器的一部分位于多维数据集本身中。当停止移动时,它会推动我,因此它们不会相交(这很有意义)。

我尝试使用.Transalte,.MovePosition并通过更改刚体本身的速度来移动播放器。没有任何改变。玩家总是可以将自己的一部分移到立方体中。

任何想法如何解决这个问题?

我的播放器控制器:(在Move()中注释掉的两行只是移动播放器的其他方式。)

using UnityEngine;

public class PlayerController : MonoBehaviour
{
   [SerializeField]
   private float movementSpeed;

   private Vector3 input;

   private void Update()
   {
      GetInput();
   }

   private void FixedUpdate()
   {
      Move();
   }

   private void GetInput()
   {
      float inputHorizontal = Input.GetAxisRaw("Horizontal");
      float intputVertical = Input.GetAxisRaw("Vertical");
      input = Vector3.ClampMagnitude(new Vector3(inputHorizontal, 0, intputVertical), 1);
   }

   private void Move()
   {
      GetComponent<Rigidbody>().velocity += input * movementSpeed;
      //GetComponent<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position + input * movementSpeed * Time.deltaTime);
      //transform.Translate(input * movementSpeed * Time.deltaTime, Space.World);
   }
}

Player is standing still

Player is moving towards cube

Settings of the Game Objects itself

unity3d game-physics collision
1个回答
0
投票

您可以从附加到其上的对撞机组件中增加玩家对撞机的比例。您可以从编辑器场景视图中检查对撞机的大小。

编辑:问题可能是您的运动或碰撞代码在Update中而不是FixedUpdate中被调用。使用刚体时,您想在FixedUpdate中调用物理计算。

© www.soinside.com 2019 - 2024. All rights reserved.