团结5个角色跳跃

时间:2016-10-26 20:03:25

标签: c# unity3d game-physics

首先感谢大家阅读我的问题, 我正在尝试创建一个像地铁表面这样的永远运行的游戏,我在update()脚本py中启动了payerControl函数,迫使刚体以10的速度向前移动,然后代码中的miningd然后我在键盘上按下向上箭头时添加了跳跃输入,但问题是角色有时会跳跃并且大部分时间不会跳跃,因为跳跃布尔值会在不到一秒的时间内变为真,然后返回到假,他们有什么办法让跳跃过程停留2秒钟?

字符控制代码:

using UnityEngine;
using System.Collections;

public class player : MonoBehaviour {

    public Animator anim;
    public Rigidbody rbody;
    public float verticalJumpPower;
    public float horizantalJumpPower;
    public float playerVelocity;


    // Use this for initialization
    void Start () 
    {
        anim = GetComponent<Animator>();
        rbody = GetComponent<Rigidbody>();
        verticalJumpPower = 50f;
        horizantalJumpPower = 30f;
        playerVelocity = 10f;
    }

    // Update is called once per frame
    void Update () 
    {
        rbody.velocity = new Vector3(rbody.velocity.x,rbody.velocity.y,playerVelocity*Time.deltaTime);
        rbody.transform.rotation = Quaternion.identity;

        if(Input.GetKeyDown (KeyCode.UpArrow))
        {
            anim.SetBool("jump",true);
            rbody.AddForce(new Vector3(0,verticalJumpPower,horizantalJumpPower));


        }
        else
        {
            anim.SetBool("jump", false);
        }

        if(Input.GetKey(KeyCode.LeftArrow))
        {
            Vector3 currentPosition = rbody.transform.position;

                Vector3 leftPosition = new Vector3(rbody.transform.position.x+Constants.PLAYER_LEFT_DISTANCE, rbody.transform.position.y,rbody.transform.position.z);

            rbody.transform.position = Vector3.Lerp(currentPosition,leftPosition,Time.deltaTime);

        }

        if(Input.GetKey(KeyCode.RightArrow))
        {
            Vector3 currentPosition = rbody.transform.position;
            Vector3 rightPosition = new Vector3(rbody.transform.position.x+Constants.PLAYER_RIGHT_DISTANCE, rbody.transform.position.y,rbody.transform.position.z);
            rbody.transform.position = Vector3.Lerp(currentPosition,rightPosition,Time.deltaTime);

        }

        if(Input.GetKey(KeyCode.DownArrow))
        {
            anim.SetBool("isSlide",true);
            rbody.velocity = new Vector3(0f, 0f,5f);
            anim.applyRootMotion = false;
        }
        else
        {
            anim.SetBool("isSlide", false);
            anim.applyRootMotion = true;
        }
    }

   }

1 个答案:

答案 0 :(得分:1)

GetKeyDown()仅在初始按键的帧中返回true。现在我期望发生的是: 一个keydown的第一帧触发跳跃动画并添加力,这一行的下一帧

rbody.velocity = new Vector3(rbody.velocity.x,rbody.velocity.y,playerVelocity*Time.deltaTime);

重置z速度然后我们点击其他条件

    else
    {
        anim.SetBool("jump", false);
    }

并关闭动画。您可能希望使跳转动画运行2秒并让它触发事件以在之后重置速度。或者你想要而不是立即运行效果,让按键改变对象的某些状态(可能存储游戏时间,之后你应该清除标志并重置速度和动画状态)