【问题标题】:Unity - character falling faster than jumping团结 - 角色下落比跳跃快
【发布时间】:2022-05-18 11:33:03
【问题描述】:

我正在尝试为角色实现我自己的重力。 但是对于一些我不知道的原因,角色的着陆速度比跳跃时高很多(~15 初始跳跃速度,~24 最终着陆速度)。我最终试图复制下面 gif 中显示的行为。

https://imgur.com/a/q77w5kS

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

public class Movement : MonoBehaviour {
    public float speed = 3;
    public float jumpSpeed = 15;
    public float gravity = -1f;


    private float ySpeed = 0;
    private float jumpTime = 0;
    private bool direction = false; //true for going up, false for falling down - gravity
    private bool previousDirection = false; //to keep track of changing direction

    private CharacterController _characterController;
    // Start is called before the first frame update
    void Start() {
        _characterController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update() {
        float dx = Input.GetAxis("Horizontal");
        float dz = Input.GetAxis("Vertical");
        float dy = Input.GetAxis("Jump");

//        move around        
        Vector3 movement = new Vector3(dx, 0, dz);

//        limit diagonal movement
        movement = Vector3.ClampMagnitude(movement, 1);
//        speed
        movement *= speed;


//        change of direction
        if(ySpeed>0 && direction!=true)
            direction = true;
        else if(ySpeed<0 && direction!=false)
            direction = false;

//        if changed direction - peak of the jump
        if(direction!=previousDirection){
            Debug.Log("Jump Time = " + jumpTime);
            jumpTime = Time.deltaTime;
            previousDirection = direction;
        }

//        jump/on ground
        if(_characterController.isGrounded) {
            ySpeed = -0.5f;
            jumpTime = 0;

            if(dy > 0f){
                ySpeed = jumpSpeed;
            }
        }
//        gravity - when not on ground
        else{
//            dv/dt
            ySpeed += gravity*jumpTime;
//            add jump time
            jumpTime += Time.deltaTime;

            // Debug.Log(jumpTime);
            Debug.Log(ySpeed);
        }

        movement.y = ySpeed;

//        direction adjustment
        movement = transform.TransformDirection(movement);

        movement *= Time.deltaTime;
        _characterController.Move(movement);
    }

}

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    我想我发现我的想法出了什么问题。 ySpeed += gravity*jumpTime

    所以每一帧我都在向下添加越来越多的加速度。这应该只是:ySpeed += gravity *Time.deltaTime (重力加速度是恒定的,不会随着时间的推移而变大)

    它通过多个步骤进行集成,因此每个 Update() 循环都是一个时间片,它会根据重力加速度增加一些速度,乘以该片所花费的时间。

    换句话说... 重力是一个恒定的加速度。我已经使它成为在空中花费的时间的线性函数。所以,我在没有重力的情况下开始跳跃,并以非常高的重力结束跳跃。

    【讨论】:

    • 根据我有限的数学知识:速度=位置变化率(即位置+=速度*时间间隔);加速度 = 速度变化率(即速度 += 加速度 * timeInterval)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多