繁体   English   中英

刚体的 addforce() function 似乎无法正常工作

[英]rigidbody's addforce() function doesn't seem to work properly

我正在尝试制作一个简单的 3d 字符 controller,但跳转根本不起作用。 我想在我的跳跃中使用刚体,但是 .addforce 不起作用。 我有一个Debug.log来查看它是否通过了跳转条件并且确实通过了。 问题在于rb.addforce() 我在下面添加了完整的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
[SerializeField] Transform camera;
[SerializeField] float sens = 3.5f;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField] [Range(0.0f, 0.5f)] float smooth = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float smoothMouse = 0.3f;
[SerializeField] float gravity = -13.0f;
[SerializeField] float slopeForce = 6;
[SerializeField] float slopeForceRayLenght = 1.5f;
[SerializeField] float runSpeed = 10;
[SerializeField] float runBuildUp = 4;
[SerializeField] float jumpForce = 10;
[SerializeField] KeyCode jumpk;
[SerializeField] KeyCode runk;

[SerializeField] bool cursorLock = true;

public Rigidbody rb;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask grndMask;

public bool isGrounded;

float cameraPitch = 0.0f;
float velocityY = 0.0f;
float speed;

Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;

Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;

CharacterController controller;

void Start()
{
    speed = walkSpeed;

    controller = GetComponent < CharacterController>();
    rb = GetComponent<Rigidbody>();
    if ( cursorLock ) {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
}

// Update is called once per frame
void Update()
{
    UpdateMouseLook();
    UpdateMovement();
    Jump();
}

void UpdateMouseLook () {
    Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"),         
Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, mouseDelta,     
ref currentMouseDeltaVelocity, smoothMouse);

    cameraPitch -= currentMouseDelta.y * sens;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
    camera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentMouseDelta.x * sens);
}

void SetSpeed () {
    if ( Input.GetKey(runk) ) {
        speed = Mathf.Lerp(speed, runSpeed, Time.deltaTime * runBuildUp);
    }else {
        speed = Mathf.Lerp(speed, walkSpeed, Time.deltaTime * runBuildUp);
    }
}

void Jump () {
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, 
grndMask);
    if ( isGrounded && Input.GetKeyDown(jumpk)) {
        Debug.Log("jumping");
        rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    }
}

public bool OnSlope () {
    RaycastHit hit;

    if ( Physics.Raycast(transform.position, Vector3.down, out hit, 
controller.height / 2 * slopeForceRayLenght) ) {
        if ( hit.normal != Vector3.up ) {
            return true;
        }
    }
    return false;
}

void UpdateMovement () {
    Vector2 inputDir = new Vector2(Input.GetAxisRaw("Horizontal"), 
 Input.GetAxisRaw("Vertical"));
    inputDir.Normalize();

    currentDir = Vector2.SmoothDamp(currentDir, inputDir, ref 
 currentDirVelocity, smooth);

    if ( controller.isGrounded ) {
        velocityY = 0.0f;
    }
    velocityY += gravity * Time.deltaTime;

    Vector3 velocity = (transform.forward * currentDir.y + transform.right 
* currentDir.x) * speed + Vector3.up * velocityY;

    controller.Move(velocity * Time.deltaTime);

    if((inputDir.x != 0 || inputDir.y != 0) && OnSlope() ) {
        controller.Move(Vector3.down * controller.height / 2 * slopeForce 
 * Time.deltaTime);
    }
    SetSpeed();
  }
}

我确保未选中is kinematic框,在刚体选项卡中,质量设置为 1,拖动为 1,angular 拖动为 0.05。 还选中了Use gravity框并将jumpk键设置为空格键(我在统一编辑器中设置)。 没有触及其他选项。 对于播放器,我使用空的 object。

可能是因为使用字符 controller 和刚体有点尴尬。 对于跳转部分,请使用:

void Jump()
{
    // your ground check code works. Keep it.
    velocityY = Mathf.Sqrt(jumpForce * 2f * gravity);
    controller.Move(0f, velocityY * Time.deltaTime, 0f)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM