mohammad
mohammad

Reputation: 2212

unity 5 character jumping

first of all thank you all for reading my question, i am trying to create a for ever running game like subway surface, i started the update() function in the payerControl script py forcing the rigid body to move forward with the speed of 10 as mintiond in the code then i added the jumping input when you press the upArrow on the keyboard , but the problem here is that the character sometimes jump and most of the time dose not jump, because the jump boolean become true for less than one second and then go back to false , so is their any way to make the jump process to stay for 2 seconds ?

Character control code :

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;
        }
    }

   }

Upvotes: 1

Views: 267

Answers (1)

John Garrard
John Garrard

Reputation: 167

GetKeyDown() returns true only in the frame of the initial keypress. Right now what I expect to happen is this: One the first frame of keydown it triggers the jump animation and adds the force, the next frame this line

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

resets the z velocity then we hit the else condition

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

and turn off the animation. You might want to make the jump animation run for 2 seconds and have it trigger an event to reset the velocity afterwards. Or you will want to instead of having the effect run immediately, have the keypress change some state of the object (perhaps store a game time after which you should clear the flag and reset the velocity and animation state)

Upvotes: 1

Related Questions