Hey guys I’ve got a request to ask could you help me implement jump into my script ? I tried so many variations and none of them worked. It is in C# and should be written on the line 65 // Y - Up and Down. Thanks for any advice !
using UnityEngine;
using System.Collections;
public class PlayerMotor : MonoBehaviour
{
private CharacterController controller;
private Vector3 moveVector;
private Vector3 moveDir = Vector3.zero;
private float speed = 5.0f;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
private float animationDuration = 3.0f;
private float startTime;
private bool isDead = false;
//Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
startTime = Time.time;
}
//Update is called once per frame
void Update()
{
if (isDead)
return;
if(Time.time - startTime < animationDuration)
{
controller.Move(Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
// X - Left and Right
moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
if(Input.GetMouseButtonDown(0))
{
if (Input.mousePosition.x > Screen.width / 2)
moveVector.x = speed;
else
moveVector.x = -speed;
}
// Y - Up and Down
moveVector.y = verticalVelocity;
// Z - Forward and Backward
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
public void SetSpeed(float modifier)
{
speed = 5.0f + modifier;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
Death();
}
private void Death()
{
isDead = true;
GetComponent<Score>().OnDeath();
}
}