2019-09-29 19:40:54 -04:00
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
|
|
// This script moves the character controller forward
|
|
|
|
|
// and sideways based on the arrow keys.
|
|
|
|
|
// It also jumps when pressing space.
|
|
|
|
|
// Make sure to attach a character controller to the same game object.
|
|
|
|
|
// It is recommended that you make only one call to Move or SimpleMove per frame.
|
|
|
|
|
|
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
CharacterController characterController;
|
2019-12-05 22:42:16 -05:00
|
|
|
CapsuleCollider capsule;
|
2019-09-29 19:40:54 -04:00
|
|
|
|
2019-12-14 23:40:14 -05:00
|
|
|
public float maxSpeed;
|
|
|
|
|
public float acceleration;
|
|
|
|
|
public float friction;
|
2019-12-05 21:57:29 -05:00
|
|
|
public float jumpHeight;
|
2019-11-10 21:04:52 -05:00
|
|
|
public float gravity;
|
|
|
|
|
public float rotationSpeed;
|
|
|
|
|
public Transform pivot;
|
|
|
|
|
public GameObject playerModel;
|
2019-09-29 19:40:54 -04:00
|
|
|
|
2019-12-31 18:17:35 -05:00
|
|
|
private bool wallRunning = false;
|
2020-01-05 01:45:45 -05:00
|
|
|
private bool diving = false;
|
2020-01-05 12:56:23 -05:00
|
|
|
private bool sliding = false;
|
2019-12-05 21:57:29 -05:00
|
|
|
private int jump = 0;
|
2019-12-31 18:17:35 -05:00
|
|
|
private int wall = 0;
|
2020-01-05 12:56:23 -05:00
|
|
|
private int dive = 0;
|
2019-12-14 23:49:23 -05:00
|
|
|
private float maxSpeedStore;
|
2020-01-04 15:52:45 -05:00
|
|
|
private float maxSpeedCap = 100;
|
2020-01-04 15:02:05 -05:00
|
|
|
private float accelerationStore;
|
2019-12-05 22:42:16 -05:00
|
|
|
private float capsuleHeight;
|
|
|
|
|
private float controllerHeight;
|
|
|
|
|
private float transformHeight;
|
2019-12-14 23:40:14 -05:00
|
|
|
private float xVelocity = 0.0f;
|
|
|
|
|
private float zVelocity = 0.0f;
|
2019-09-29 19:40:54 -04:00
|
|
|
private Vector3 moveDirection;
|
2019-12-14 23:40:14 -05:00
|
|
|
private Vector3 velocity;
|
2020-01-05 01:45:45 -05:00
|
|
|
private Vector3 gravityVelocity;
|
2019-09-29 19:40:54 -04:00
|
|
|
|
2020-01-05 12:56:23 -05:00
|
|
|
float slideTime = 0.0f;
|
|
|
|
|
float wallRunTime = 0.0f;
|
2019-12-05 23:17:24 -05:00
|
|
|
float oneSec = 1.0f;
|
2020-01-05 01:45:45 -05:00
|
|
|
float halfSec = 0.5f;
|
|
|
|
|
float quarterSec = 0.25f;
|
2019-12-05 23:17:24 -05:00
|
|
|
|
2019-09-29 19:40:54 -04:00
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
characterController = GetComponent<CharacterController>();
|
2019-12-05 22:42:16 -05:00
|
|
|
capsule = GetComponent<CapsuleCollider>();
|
|
|
|
|
transformHeight = transform.localScale.y;
|
|
|
|
|
controllerHeight = characterController.height;
|
|
|
|
|
capsuleHeight = capsule.height;
|
2019-12-14 23:49:23 -05:00
|
|
|
maxSpeedStore = maxSpeed;
|
2020-01-04 15:02:05 -05:00
|
|
|
accelerationStore = acceleration;
|
2019-09-29 19:40:54 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 23:17:24 -05:00
|
|
|
void OnControllerColliderHit(ControllerColliderHit hit)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(hit.gameObject.tag == "Wall")
|
|
|
|
|
{
|
2019-12-31 18:17:35 -05:00
|
|
|
wallRunning = true;
|
|
|
|
|
wall++;
|
2019-12-21 23:17:24 -05:00
|
|
|
}
|
2019-12-31 18:17:35 -05:00
|
|
|
|
2019-12-21 23:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
2019-09-29 19:40:54 -04:00
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
float yStore = moveDirection.y;
|
2019-12-14 23:40:14 -05:00
|
|
|
moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
|
|
|
|
|
moveDirection = transform.TransformDirection(moveDirection);
|
|
|
|
|
moveDirection = Vector3.ClampMagnitude(moveDirection, 1.0f);
|
2020-01-05 01:45:45 -05:00
|
|
|
moveDirection.y = yStore;
|
2019-09-29 19:40:54 -04:00
|
|
|
|
2020-01-04 16:15:15 -05:00
|
|
|
if (jump >= 2)
|
2019-09-29 19:40:54 -04:00
|
|
|
{
|
2020-01-05 01:45:45 -05:00
|
|
|
if(maxSpeed > maxSpeedStore/1.5f)
|
2020-01-04 15:52:45 -05:00
|
|
|
{
|
2020-01-04 16:15:15 -05:00
|
|
|
maxSpeed -= acceleration;
|
2020-01-04 15:52:45 -05:00
|
|
|
}
|
2019-09-29 19:40:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (characterController.isGrounded)
|
|
|
|
|
{
|
2020-01-05 01:45:45 -05:00
|
|
|
diving = false;
|
2020-01-05 12:56:23 -05:00
|
|
|
dive = 0;
|
2019-12-05 21:57:29 -05:00
|
|
|
jump = 0;
|
2019-09-29 19:40:54 -04:00
|
|
|
moveDirection.y = 0.0f;
|
|
|
|
|
|
2020-01-04 16:15:15 -05:00
|
|
|
if(maxSpeed > maxSpeedStore)
|
|
|
|
|
{
|
|
|
|
|
maxSpeed -= acceleration/10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(maxSpeed < maxSpeedStore && !Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.LeftControl))
|
|
|
|
|
{
|
|
|
|
|
maxSpeed += acceleration;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-30 21:15:20 -04:00
|
|
|
if(Input.GetKey(KeyCode.LeftShift))
|
|
|
|
|
{
|
2020-01-04 15:52:45 -05:00
|
|
|
if(maxSpeed > maxSpeedStore/2)
|
|
|
|
|
{
|
|
|
|
|
maxSpeed -= acceleration;
|
|
|
|
|
}
|
2019-09-30 21:15:20 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-05 22:42:16 -05:00
|
|
|
if(Input.GetKey(KeyCode.LeftControl))
|
|
|
|
|
{
|
2019-12-06 22:34:57 -05:00
|
|
|
|
|
|
|
|
characterController.height /= 2;
|
|
|
|
|
capsule.height /= 2;
|
|
|
|
|
transform.localScale = new Vector3(transform.localScale.x, transformHeight/2, transform.localScale.z);
|
|
|
|
|
|
2020-01-05 12:56:23 -05:00
|
|
|
if(Input.GetKeyDown(KeyCode.LeftControl))
|
2019-12-05 23:17:24 -05:00
|
|
|
{
|
2020-01-05 12:56:23 -05:00
|
|
|
if((Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0))
|
|
|
|
|
{
|
|
|
|
|
sliding = false;
|
|
|
|
|
} else {
|
|
|
|
|
sliding = true;
|
|
|
|
|
slideTime = Time.time;
|
|
|
|
|
}
|
2019-12-21 21:00:36 -05:00
|
|
|
}
|
2019-12-05 23:17:24 -05:00
|
|
|
|
2020-01-05 12:56:23 -05:00
|
|
|
if(slideTime + halfSec >= Time.time && sliding == true)
|
2019-12-05 23:17:24 -05:00
|
|
|
{
|
2019-12-06 22:34:57 -05:00
|
|
|
|
2020-01-04 15:52:45 -05:00
|
|
|
if(maxSpeed < maxSpeedStore*2)
|
|
|
|
|
{
|
2020-01-04 16:15:15 -05:00
|
|
|
maxSpeed += acceleration*2;
|
2020-01-04 15:52:45 -05:00
|
|
|
}
|
2020-01-05 01:45:45 -05:00
|
|
|
|
2020-01-05 12:56:23 -05:00
|
|
|
if(Input.GetButton("Jump") && slideTime + quarterSec >= Time.time)
|
2020-01-05 01:45:45 -05:00
|
|
|
{
|
|
|
|
|
maxSpeed += 5;
|
|
|
|
|
}
|
2019-12-06 22:34:57 -05:00
|
|
|
|
2019-12-14 23:40:14 -05:00
|
|
|
} else {
|
2019-12-06 22:34:57 -05:00
|
|
|
|
2020-01-04 15:52:45 -05:00
|
|
|
if(maxSpeed > maxSpeedStore/4)
|
|
|
|
|
{
|
|
|
|
|
maxSpeed -= acceleration;
|
|
|
|
|
}
|
2019-12-14 23:40:14 -05:00
|
|
|
|
2019-12-05 23:17:24 -05:00
|
|
|
}
|
2019-12-06 22:34:57 -05:00
|
|
|
|
2019-12-05 22:42:16 -05:00
|
|
|
} else {
|
2020-01-04 15:52:45 -05:00
|
|
|
|
2019-12-05 22:42:16 -05:00
|
|
|
characterController.height = controllerHeight;
|
|
|
|
|
capsule.height = capsuleHeight;
|
|
|
|
|
transform.localScale = new Vector3(transform.localScale.x, transformHeight, transform.localScale.z);
|
2020-01-04 15:52:45 -05:00
|
|
|
|
2019-12-05 22:42:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-05 01:45:45 -05:00
|
|
|
} else {
|
2020-01-05 12:56:23 -05:00
|
|
|
slideTime = Time.time;
|
2020-01-05 01:45:45 -05:00
|
|
|
|
|
|
|
|
if(Input.GetKey(KeyCode.LeftControl))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
characterController.height /= 2;
|
|
|
|
|
capsule.height /= 2;
|
|
|
|
|
transform.localScale = new Vector3(transform.localScale.x, transformHeight/2, transform.localScale.z);
|
|
|
|
|
diving = true;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
characterController.height = controllerHeight;
|
|
|
|
|
capsule.height = capsuleHeight;
|
|
|
|
|
transform.localScale = new Vector3(transform.localScale.x, transformHeight, transform.localScale.z);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 12:56:23 -05:00
|
|
|
//Might want to change move direction value to make triggering this speed up effect easier/harder
|
|
|
|
|
if(diving && moveDirection.y > 0.75 && Input.GetKeyDown(KeyCode.LeftControl))
|
|
|
|
|
{
|
|
|
|
|
dive++;
|
|
|
|
|
if(dive == 1){
|
|
|
|
|
maxSpeed += 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 21:00:36 -05:00
|
|
|
}
|
2019-12-14 23:40:14 -05:00
|
|
|
|
2019-12-05 21:57:29 -05:00
|
|
|
if (Input.GetButtonDown("Jump") && jump <= 1)
|
|
|
|
|
{
|
|
|
|
|
moveDirection.y = jumpHeight;
|
|
|
|
|
jump++;
|
2019-09-29 19:40:54 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 23:17:24 -05:00
|
|
|
if (characterController.collisionFlags == CollisionFlags.None)
|
|
|
|
|
{
|
2019-12-31 18:17:35 -05:00
|
|
|
wallRunning = false;
|
|
|
|
|
wall = 0;
|
2019-12-21 23:17:24 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-05 21:57:29 -05:00
|
|
|
moveDirection.y += Physics.gravity.y * gravity * Time.deltaTime;
|
2019-12-14 23:40:14 -05:00
|
|
|
|
2019-12-31 18:17:35 -05:00
|
|
|
if (wallRunning)
|
|
|
|
|
{
|
2020-01-04 15:52:45 -05:00
|
|
|
|
|
|
|
|
if(maxSpeed < maxSpeedStore*1.5f)
|
|
|
|
|
{
|
|
|
|
|
maxSpeed += acceleration;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-31 18:17:35 -05:00
|
|
|
jump = 0;
|
|
|
|
|
|
|
|
|
|
if(wall == 1)
|
|
|
|
|
{
|
2020-01-05 12:56:23 -05:00
|
|
|
wallRunTime = Time.time;
|
2019-12-31 18:17:35 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-05 12:56:23 -05:00
|
|
|
if(wallRunTime + oneSec < Time.time)
|
2019-12-31 18:17:35 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
moveDirection.y += Physics.gravity.y * (gravity/8) * Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
moveDirection.y = 0.0f;
|
|
|
|
|
}
|
2020-01-04 15:02:05 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-04 15:52:45 -05:00
|
|
|
velocity.x += moveDirection.x;
|
|
|
|
|
velocity.z += moveDirection.z;
|
2020-01-04 15:02:05 -05:00
|
|
|
|
2020-01-04 15:52:45 -05:00
|
|
|
if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0){
|
2020-01-04 15:02:05 -05:00
|
|
|
|
2020-01-04 15:52:45 -05:00
|
|
|
//Remove or "lower" friction to add an 'ice' effect
|
|
|
|
|
velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction);
|
|
|
|
|
velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction);
|
2020-01-04 15:02:05 -05:00
|
|
|
|
2020-01-05 01:45:45 -05:00
|
|
|
}
|
2020-01-04 15:02:05 -05:00
|
|
|
|
2020-01-04 15:52:45 -05:00
|
|
|
if(maxSpeed > maxSpeedCap)
|
|
|
|
|
{
|
|
|
|
|
maxSpeed = maxSpeedCap;
|
2020-01-04 15:02:05 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-04 15:52:45 -05:00
|
|
|
if(characterController.velocity == new Vector3(0, 0, 0))
|
|
|
|
|
{
|
|
|
|
|
maxSpeed = maxSpeedStore;
|
2020-01-04 15:02:05 -05:00
|
|
|
}
|
2019-12-31 18:17:35 -05:00
|
|
|
|
2019-12-14 23:40:14 -05:00
|
|
|
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
|
2019-09-29 19:40:54 -04:00
|
|
|
|
2019-12-21 21:00:36 -05:00
|
|
|
velocity.y = moveDirection.y;
|
|
|
|
|
|
2020-01-05 01:45:45 -05:00
|
|
|
Debug.Log(moveDirection.y+"\n");
|
|
|
|
|
|
|
|
|
|
if(moveDirection.y <= -5 && Mathf.Abs(velocity.x) <= 1.5f && Mathf.Abs(velocity.z) <= 1.5f)
|
|
|
|
|
{
|
|
|
|
|
velocity.x = velocity.x * Mathf.Abs(moveDirection.y);
|
|
|
|
|
velocity.z = velocity.z * Mathf.Abs(moveDirection.y);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 19:40:54 -04:00
|
|
|
// Move the controller
|
2019-12-14 23:40:14 -05:00
|
|
|
characterController.Move(velocity * Time.deltaTime);
|
2019-11-10 21:04:52 -05:00
|
|
|
|
|
|
|
|
//Move the player in different directions based on camera look direction
|
2019-11-17 20:17:16 -05:00
|
|
|
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
|
2019-11-10 21:04:52 -05:00
|
|
|
{
|
|
|
|
|
transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
|
|
|
|
|
Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
|
|
|
|
|
playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotationSpeed * Time.deltaTime);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 19:40:54 -04:00
|
|
|
}
|
|
|
|
|
}
|