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.