简体   繁体   中英

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

I am trying to make a simple 3d character controller, but the jump doesn't work at all. I want to use rigidbody in my jump but the.addforce does not work. I have a Debug.log to see if it passes the jump condition and it does. The problem is with the rb.addforce() . I added the full code below:

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

I made sure that the is kinematic box is unchecked and in the rigidbody tab, the mass is set to 1, drag to 1 and angular drag to 0.05. Also the Use gravity box is checked and the jumpk key is set to the space bar (I set it in the unity editor). Didn't touch the other options. For the player I use an empty object.

It might be because it is kind of awkward to use character controller and rigid body. For the jump part use this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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