unity3d 在统一体中,为什么我的角色在反对对象时不会倒下?

fnx2tebb  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(202)

在unity 3d中,当我跳跃时,我的角色在半空中,如果我从正确的方向按a/s/d/w进入一个物体,角色将停留在空中,只要我抓住它,我如何使它,使角色不会停留在半空中,如果我抓住一个物体的移动按钮,而不会破坏当前的移动系统和变量
下面是我动作脚本:

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

public class movement : MonoBehaviour
{
    public Rigidbody rb;
    public float MouseSensitivity;
    public float MoveSpeed;
    public float jumpForce;
    public float fallMultiplayer = 2.0f;
    // Smoothing factors
    public float moveSmoothing = 0.1f;
    public float rotateSmoothing = 0.1f;

    //animator 
    public Animator anim;
    bool isJumping = false;
    //
    private Vector3 targetMoveDirection;
    private Quaternion targetRotation;

    void Start()
    {
        //Cursor.visible = false;
    }

    void Update()
    {
        anim.SetBool("jump",false);
        //Look around
        Quaternion rotationDelta = Quaternion.Euler(new Vector3(0, Input.GetAxis("Mouse X") * MouseSensitivity, 0));
        targetRotation = rb.rotation * rotationDelta;
        rb.MoveRotation(Quaternion.Lerp(rb.rotation, targetRotation, rotateSmoothing));

        //vertical parameter 
        anim.SetFloat("vertical moving", Input.GetAxis("Vertical"));
        anim.SetFloat("horizontal", Input.GetAxis("Horizontal"));
    
        //Move
        targetMoveDirection = transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal");
        rb.AddForce(targetMoveDirection * MoveSpeed, ForceMode.VelocityChange);
        rb.velocity = Vector3.Lerp(rb.velocity, Vector3.zero, moveSmoothing);

        //Jump
        if (anim.GetCurrentAnimatorStateInfo(0).IsName("Jump"))
        {
            isJumping = true;
        }
        else
        {
            isJumping = false;
        }
        if (Input.GetKeyDown(KeyCode.Space) && Mathf.Abs(rb.velocity.y) < 0.01f && !isJumping)
        {
            anim.SetBool("jump",true);
            rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
            rb.AddForce(Physics.gravity, ForceMode.Acceleration);
        }
        
    }

    private void FixedUpdate()
    {
        if (rb.velocity.y < 0)
        {
            rb.velocity += Vector3.up * Physics.gravity.y * fallMultiplayer * Time.deltaTime;
        }
    }
}
bnlyeluc

bnlyeluc1#

如果你不希望显著修改移动代码,那么一个可能的解决方案可能是将墙壁的摩擦力减小到0,因为玩家角色刚体最有可能与墙壁发生一些摩擦,导致其停止下落。
尝试创建Physics3D材质,将其摩擦力值设置为零(或另一个足够低的值),然后确保将其指定给墙。

相关问题