How to apply jump both to controller and player for an endless runner 3D in unity?

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.

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

@AshAl1 when I started I used code that is available in Unity’s sample projects, it comes with the download of unity or can be downloaded from the site. there is also code samples in the API. unity provides everything u need to get started.

Hi All, Finally found the answer to my question. It was right under my nose. :smiley: Thanks @mar10 for the advice.

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 code. There are may ways to add jump and animations to work more realistic.