How to apply jump both to controller and player fo

2019-09-02 07:48发布

问题:

I'm trying to develop a 3D endless runner game in unity 5. The problem I'm facing is jump. How can I apply a jump to an endless runner? I've been searching and surfing through the internet with no luck. I did find some codes but when I try to apply them, they didn't work. Also How do I adjust the character controller to go up with the animation? A help would be really appreciated.

This is the playerMotor.cs code.

    private Vector3 moveVector;
    private float verticalVelocity = 0.0f;
    private float gravity = 12.0f;
    private float jumpPower = 15.0f;
    private bool jump;

    private float animationDuration = 3.0f;
    private float startTime;
    private Animator anim; 
    private CharacterController controller;

void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
        startTime = Time.time;
    }

void Update()
    {

        if (Time.time - startTime < animationDuration)
        {
            controller.Move(Vector3.forward * speed * Time.deltaTime);
            return;
        }
        moveVector = Vector3.zero;

        if (controller.isGrounded)
        {
           verticalVelocity = -0.5f;
            if (Input.GetButton("Jump"))
            {
                verticalVelocity = jumpPower;
                anim.SetBool("Jump", true);
            }
            //anim.SetBool("Jump", false);       

        }
        else
        {
            verticalVelocity -= gravity * Time.deltaTime;

        }

        // X - Left and Right
        moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
        anim.SetFloat("Turn", moveVector.x);

        if (Input.GetMouseButton(0))
        {
            //Are we holding touch on the right side?
            if (Input.mousePosition.x > Screen.width / 2)
            {
                moveVector.x = speed;
            }
            else
            {
                moveVector.x = -speed;
            }
        }

        //// Y - Up and Down
        //if (Input.GetButtonDown("Jump"))
        //{
        //    moveVector.y = verticalVelocity;
        //}

        // Z - Forward and Backward
        moveVector.z = speed;

        controller.Move(moveVector * Time.deltaTime);
}

The codes I used to implement is shown below.

function PlayerController(){
         var controller : CharacterController = GetComponent(CharacterController);
         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         if (controller.isGrounded) {
             vSpeed = -1;
             if (Input.GetButtonDown ("Jump")) {
                 vSpeed = jumpSpeed;
             }
         }
         vSpeed -= gravity * Time.deltaTime;
         moveDirection.y = vSpeed;
         controller.Move(moveDirection * Time.deltaTime);
}

I have applied jump for normal RPG characters but this one is harder than I thought.

回答1:

From your comment it seems your actual problem is "controller.isGrounded" never ticks in, right?
I described the first issue in comment (anim.SetBool("Jump", false);) related to animation.
This second problem comes from your PlayerController code.

You "reset" your jump with setting vSpeed = -1;, right after the frame you just jumped. So techincally, the engine doesn't even have time to react on the jump as the character the next frame gets pulled back hard to the ground (by the "anti-jump" :) you implemented).

What I recommend is, take the sample code from CharacterController.Move, just like you did before you applied your changes, but this time don't alter it!
Just copy-paste the snippet into your app and test. After you made it work "as is", again, without any changes in the code, add the customizations you want, one by one and test every time if the change introduced a defect (bug).


I'd also recommend you to start using Debug.Log() while you are coding so you can filter out issues like what you have now, as you'll see what happens in the code "on the fly", when you play-test (.Log variable values with comments, if "branches" when they tick in, calculated values, calls to important functions -like .Move()- etc).


I hope this helps! Cheers!



回答2:

I finally found the answer to my question. Thanks Mark for the advice you gave me, it helped me a lot.

So here's what I did to make the jump and the jump animation happen. Also note that I'm posting what which is correct not the whole code since I didn't find any other errors in the rest of the script.

        moveVector = Vector3.zero;

        if (controller.isGrounded) //Check whether are we ground
        {

            verticalVelocity = -0.5f; //Just to make sure that we are ground
            if (Input.GetButton("Jump"))
            {

                verticalVelocity = 10f; //Basically this is the Jump power to Player
                anim.SetBool("Jump", true); //Jump animation to true
            }
        }
        else
        {
            verticalVelocity -= gravity * speed * Time.deltaTime; //Apply gravity
            anim.SetBool("Jump", false); //Set jump animation to false to stop looping continuously 
        }

//... the rest

This is my own code. Thanks for helping Mark. Cheers to you too.