【问题标题】:Unity jump issueunity跳转问题
【发布时间】:2022-01-08 19:51:25
【问题描述】:

我制作了第三人称游戏,其中我有用于控制我的角色模型的脚本。 问题是当我跳跃而不按任何按钮时,他会在空中堆叠,但如果我按下控制按钮跳跃,他工作正常。 提前致谢。

播放器控制器脚本:

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

[RequireComponent(typeof(CharacterController))]
public class RelativeMovement : MonoBehaviour
{
    [SerializeField] private Transform target;

    public float rotSpeed = 5.0f;
    public float moveSpeed = 6.0f;

    public float jumpSpeed = 15.0f;
    public float gravity = -9.8f;
    public float terminalVelocity = -20.0f;
    public float minFall = -1.5f;

    private float _vertSpeed;

    private CharacterController _charController;

    private ControllerColliderHit _contact;

    void Start()
    {
        _vertSpeed = minFall;
        _charController = GetComponent<CharacterController>();
    }

    void Update()
    {
        Vector3 movement = Vector3.zero;

        float horInput = Input.GetAxis("Horizontal");
        float vertInput = Input.GetAxis("Vertical");

        if(horInput != 0 || vertInput != 0)
        {
            movement.x = horInput * moveSpeed;
            movement.z = vertInput * moveSpeed;
            movement = Vector3.ClampMagnitude(movement, moveSpeed);

            Quaternion tmp = target.rotation;
            target.eulerAngles = new Vector3(0, target.eulerAngles.y, 0);
            movement = target.TransformDirection(movement);
            target.rotation = tmp;

            Quaternion direction = Quaternion.LookRotation(movement);
            transform.rotation = Quaternion.Lerp(transform.rotation, 
                direction, rotSpeed * Time.deltaTime);

            bool hitGround = false;
            RaycastHit hit;
            if(_vertSpeed < 0 && Physics.Raycast(transform.position, Vector3.down, out hit))
            {
                float check =
                    (_charController.height + _charController.radius) / 1.9f;
                hitGround = hit.distance <= check;
            }

            if(hitGround)
            {
                if (Input.GetButtonDown("Jump"))
                {
                    _vertSpeed = jumpSpeed;
                }
                else
                {
                    _vertSpeed = minFall;
                }
            }
            else
            {
                _vertSpeed += gravity * 5 * Time.deltaTime;
                if(_vertSpeed < terminalVelocity)
                {
                    _vertSpeed = terminalVelocity;
                }

                if(_charController.isGrounded)
                {
                    if(Vector3.Dot(movement, _contact.normal) < 0)
                    {
                        movement = _contact.normal * moveSpeed;
                    }
                    else
                    {
                        movement += _contact.normal * moveSpeed;
                    }
                }
            }

            movement.y = _vertSpeed;
            movement *= Time.deltaTime;
            _charController.Move(movement);
        }
    }

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        _contact = hit;
    }
}

耶稣照片: enter image description here

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我没有浏览所有代码,但我可以立即看到导致您描述的问题的原因:

    你的整个逻辑都发生在这个 if 语句下:

    if(horInput != 0 || vertInput != 0)
    

    因此,只有当您主动按下移动按钮时,此逻辑块中的所有内容才会发生。

    您可以删除它(或者更好的是,在正确的位置关闭 if 块。我认为它会向下 2 行)

    【讨论】:

    • 但理论上,如果没有输入,它应该下降,如果有一个else,它将为此打印。还是我什么都不懂?
    • 哦,没关系,我明白你的意思了,非常感谢,你帮了我很大的忙
    【解决方案2】:

    这个问题的答案是:

     if((horInput == 0 || vertInput == 0) || (horInput != 0 && vertInput != 0))
        {
            movement.x = horInput * moveSpeed;
            movement.z = vertInput * moveSpeed;
            if(horInput != 0 || vertInput != 0)
            {
                movement = Vector3.ClampMagnitude(movement, moveSpeed);
    
                Quaternion tmp = target.rotation;
                target.eulerAngles = new Vector3(0, target.eulerAngles.y, 0);
                movement = target.TransformDirection(movement);
                target.rotation = tmp;
    
                Quaternion direction = Quaternion.LookRotation(movement);
                transform.rotation = Quaternion.Lerp(transform.rotation,
                    direction, rotSpeed * Time.deltaTime);
            }
    

    谢谢乔

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多