如何为玩家动作添加碰撞? (团结)

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

我这里有一段代码可以让玩家朝摄像机的方向移动。动作有效,但玩家穿过墙壁,我尝试向 p[层添加一个盒子对撞机,但那没有用。我尝试添加一个刚体,但它限制了我在 x 和 z 轴上的运动,但我的游戏设置在水下,我需要玩家能够在面朝上的情况下游上去。

有人可以帮助我吗。

using UnityEngine;

使用系统集合;

公共课 Movement1 : MonoBehaviour {

/* 
wasd : basic movement
shift : Makes camera accelerate */
 
public float sensX;
public float sensY;
public Transform orientation;

float xRotation;
float yRotation;
float mainSpeed = 5.0f; //regular speed
float shiftAdd = 8.0f; //multiplied by how long shift is held.  Running
float maxShift = 12.0f; //Maximum speed when holdin gshift
float camSens = 0.25f; //How sensitive the mouse is
private Vector3 lastMouse = new Vector3(255, 255, 255); //sets mouse near the middle
private float totalRun= 1.0f;

private void Start()
    {
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
}

void Update () {
    float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
  float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;

  yRotation += mouseX;
  xRotation -= mouseY;
  xRotation = Mathf.Clamp(xRotation, -90f, 90f);

  transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
  orientation.rotation = Quaternion.Euler(0, yRotation, 0);
   
    //Keyboard commands
    float f = 0.0f;
    Vector3 p = GetBaseInput();
    if (p.sqrMagnitude > 0){ // only move while a direction key is pressed
      if (Input.GetKey (KeyCode.LeftShift)){
          totalRun += Time.deltaTime;
          p  = p * totalRun * shiftAdd;
          p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
          p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
          p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
      } else {
          totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
          p = p * mainSpeed;
      }
            
      p = p * Time.deltaTime;
      Vector3 newPosition = transform.position;
      if (Input.GetKey(KeyCode.Space)){ //If player wants to move on X and Z axis only
          transform.Translate(p);
          newPosition.x = transform.position.x;
          newPosition.z = transform.position.z;
          transform.position = newPosition;
      } else {
          transform.Translate(p);
      }
    }
}

private Vector3 GetBaseInput() { //returns the basic values, if it's 0 than it's not active.
    Vector3 p_Velocity = new Vector3();
    if (Input.GetKey (KeyCode.W)){
        p_Velocity += new Vector3(0, 0 , 1);
    }
    if (Input.GetKey (KeyCode.S)){
        p_Velocity += new Vector3(0, 0, -1);
    }
    if (Input.GetKey (KeyCode.A)){
        p_Velocity += new Vector3(-1, 0, 0);
    }
    if (Input.GetKey (KeyCode.D)){
        p_Velocity += new Vector3(1, 0, 0);
    }
    return p_Velocity;
}

}

c# unity3d user-controls collision
1个回答
0
投票

刚体的概念相信大家可能不是很清楚。当您将 rigidBody 组件添加到您的对象时,这意味着它现在受到重力和其他统一物理力学的影响,例如阻力和力。

所以,如果你正在使用刚体,用

transform.position += ...
改变玩家位置可能不是一个好主意,你应该使用
rigiBody.AddForce()
,这样统一物理引擎会为你计算运动。

其他选项,可能更适合您的情况是禁用刚体中的重力,将参数重力比例设置为 0。这样您就可以使用

transform.position += ...
在所有轴上自由移动刚体。你可能正在挣扎,因为当你试图改变它的 Y 位置时,重力将你的刚体向下推。

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