using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonScript : MonoBehaviour
{
float speed = 4;
float rotSpeed = 140;
float rot = 0f;
float gravity = 8;
float jumpForce = 8.0f;
float verticalVelocity;
bool is_in_air = false;
float condition;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded)
{
verticalVelocity = gravity * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = jumpForce;
}
anim.SetBool("is_in_air", false);
anim.SetInteger("condition", 0);
if (is_in_air)
{
anim.SetInteger("condition", 1);
}
if (Input.GetKey(KeyCode.W))
{
anim.SetInteger("condition", 1);
moveDir = new Vector3(0, 0, 1);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
if (Input.GetKeyUp(KeyCode.W))
{
anim.SetInteger("condition", 0);
moveDir = new Vector3(0, 0, 0);
}
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
{
anim.SetInteger("condition", 2);
speed = 7;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
anim.SetInteger("condition", 0);
speed = 4;
}
if (Input.GetKey(KeyCode.Space))
{
anim.SetBool("is_in_air", true);
anim.SetInteger("condition", 3);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.Space))
{
anim.SetInteger("condition", 3);
}
}
else
{
anim.SetBool("is_in_air", true);
anim.SetInteger("condition", 3);
moveDir = new Vector3(0, 0, 0);
}
Vector3 moveVector = new Vector3(0, verticalVelocity, 0);
controller.Move(moveVector * Time.deltaTime);
rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rot, 0);
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}