跳前延迟

时间:2019-06-19 13:39:39

标签: c# unity3d 3d

我正在学习Unity,并且正在做角色移动,但是跳跃的动画会延迟字符从字面上跳跃的时间,直到它被拾取之前为0.30s,我如何在代码?

就像我想过的那样,当您按下“ Space”时释放动画,计数0.20s并进行跳跃。可行吗?我该怎么办?

简而言之,角色跳到动画之前。

动画视频: https://imgur.com/a/LgzkSKi

public class PlayerController : MonoBehaviour {

    public float moveSpeed;
    public float jumpForce;
    public CharacterController controller;

    private Vector3 moveDirection;
    public float gravityScale;

    public Animator animator;

    void Start() {
        controller = GetComponent<CharacterController>();
    }

    void Update() {
        float yStore = moveDirection.y;
        moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
        moveDirection = moveDirection.normalized * moveSpeed;
        moveDirection.y = yStore;

        jump();

        controller.Move(moveDirection * Time.deltaTime);
        animator.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical"))));

    }

    void jump() {
        if (controller.isGrounded) {
            moveDirection.y = 0f;
            if (Input.GetButtonDown("Jump")) {
                moveDirection.y = jumpForce;
                Debug.Log("jump");
            }
        }
        moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
        animator.SetBool("isGrounded", controller.isGrounded);
    }
}

2 个答案:

答案 0 :(得分:1)

在您的jump()函数中不要施加跳跃力。而是在下一次该角色跳动时进行设置。

float nextJumpTime;
bool todoJump;
void jump() {
    if (!todoJump && Input.GetButtonDown("Jump")) {
        // Remember we have to jump and when
        nextJumpTime = Time.time + 0.2f;
        todoJump = true;
    }
    // Execute the jump
    if (todoJump && Time.time >= nextJumpTime) {
        todoJump = false;
        moveDirection.y = jumpForce;
    }
}

可以选择协程阅读。在输入上启动协程,产生返回新的WaitForSecond(0.2f);在协程中,然后执行跳转。

答案 1 :(得分:0)

关于一个人Mokey Squad的建议,我认为您还应该考虑获取新的跳转动画或修剪当前动画。 我认为为该残破的跳转动画创建自定义代码不是一个好主意,因为您将无法重用脚本。