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-11-10 21:04:52 -05:00
|
|
|
public float speed;
|
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;
|
2019-12-05 21:57:29 -05:00
|
|
|
private int jump = 0;
|
2019-12-31 18:17:35 -05:00
|
|
|
private int wall = 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;
|
2019-09-29 19:40:54 -04:00
|
|
|
|
2019-12-05 23:17:24 -05:00
|
|
|
float startTime = 0.0f;
|
|
|
|
|
float oneSec = 1.0f;
|
|
|
|
|
|
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);
|
2019-09-29 19:40:54 -04:00
|
|
|
|
2020-01-04 15:52:45 -05:00
|
|
|
if (jump > 2)
|
2019-09-29 19:40:54 -04:00
|
|
|
{
|
2020-01-04 15:52:45 -05:00
|
|
|
if(maxSpeed > maxSpeedStore/3)
|
|
|
|
|
{
|
|
|
|
|
maxSpeed -= acceleration*3;
|
|
|
|
|
}
|
2019-09-29 19:40:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
moveDirection.y = yStore;
|
|
|
|
|
|
|
|
|
|
if (characterController.isGrounded)
|
|
|
|
|
{
|
2019-12-05 21:57:29 -05:00
|
|
|
jump = 0;
|
2019-09-29 19:40:54 -04:00
|
|
|
moveDirection.y = 0.0f;
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.LeftControl))
|
|
|
|
|
{
|
|
|
|
|
while(maxSpeed < maxSpeedStore)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2019-12-21 21:00:36 -05:00
|
|
|
if(Input.GetKeyDown(KeyCode.LeftControl) && characterController.velocity != new Vector3(0, 0, 0)
|
|
|
|
|
&& (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
|
2019-12-05 23:17:24 -05:00
|
|
|
{
|
|
|
|
|
startTime = Time.time;
|
2019-12-21 21:00:36 -05:00
|
|
|
}
|
2019-12-05 23:17:24 -05:00
|
|
|
|
2019-12-11 21:12:47 -05:00
|
|
|
if(startTime + oneSec >= Time.time)
|
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)
|
|
|
|
|
{
|
|
|
|
|
maxSpeed += acceleration;
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
//maxSpeed = maxSpeedStore;
|
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
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
startTime = Time.time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(startTime + oneSec < Time.time)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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-04 15:52: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;
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|