2022-01-28 19:00:56 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
|
|
{
|
|
|
|
public float walkSpeed = 7.5f;
|
|
|
|
public float runningSpeed = 7.5f;
|
|
|
|
public float jumpSpeed = 7.5f;
|
|
|
|
public float lookSpeed = 7.5f;
|
|
|
|
public float lookXLimit = 7.5f;
|
|
|
|
public Camera playerCamera;
|
2022-01-29 00:39:17 +00:00
|
|
|
public ParticleSystem runningParticles;
|
2022-01-28 19:00:56 +00:00
|
|
|
Vector3 moveDirection = Vector3.zero;
|
|
|
|
public float rotationX = 0;
|
2022-01-29 02:15:33 +00:00
|
|
|
public Transform mainObject;
|
2022-01-28 19:00:56 +00:00
|
|
|
// Start is called before the first frame update
|
|
|
|
[HideInInspector]
|
|
|
|
bool canMove = true;
|
|
|
|
Rigidbody rb;
|
2022-01-28 20:05:41 +00:00
|
|
|
float moveDirectionY;
|
2022-01-28 19:00:56 +00:00
|
|
|
public Vector3 jump;
|
|
|
|
public float jumpForce = 2.0f;
|
2022-01-29 09:57:53 +00:00
|
|
|
private bool isGrounded = false;
|
2022-01-29 01:01:10 +00:00
|
|
|
public bool isRunning = false;
|
2022-01-28 19:00:56 +00:00
|
|
|
|
2022-01-29 02:15:33 +00:00
|
|
|
private Vector3 follow = Vector3.zero;
|
|
|
|
|
2022-01-28 19:00:56 +00:00
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
rb = GetComponent<Rigidbody>();
|
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
Cursor.visible = false;
|
|
|
|
jump = new Vector3(0.0f, 2.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
|
|
|
|
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
|
|
|
|
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
|
|
|
|
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
|
|
|
|
|
2022-01-29 01:01:10 +00:00
|
|
|
isRunning = Input.GetKey(KeyCode.LeftShift);
|
2022-01-28 19:00:56 +00:00
|
|
|
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
|
|
|
|
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
|
|
|
|
|
2022-01-28 20:05:41 +00:00
|
|
|
moveDirection = (transform.forward * curSpeedX * Time.deltaTime) + (transform.right * curSpeedY * Time.deltaTime);
|
2022-01-28 19:00:56 +00:00
|
|
|
|
2022-01-29 01:01:10 +00:00
|
|
|
if (isRunning && !runningParticles.isPlaying && Input.GetKey(KeyCode.W))
|
2022-01-29 00:39:17 +00:00
|
|
|
{
|
2022-01-29 01:01:10 +00:00
|
|
|
runningParticles.Play(true);
|
2022-01-29 00:39:17 +00:00
|
|
|
}
|
2022-01-29 01:01:10 +00:00
|
|
|
else if ((!isRunning && runningParticles.isPlaying) || !Input.GetKey(KeyCode.W))
|
2022-01-29 00:39:17 +00:00
|
|
|
{
|
2022-01-29 01:01:10 +00:00
|
|
|
runningParticles.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
|
2022-01-29 00:39:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 09:57:53 +00:00
|
|
|
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
|
2022-01-28 19:00:56 +00:00
|
|
|
{
|
|
|
|
Debug.Log("Jump");
|
2022-01-28 20:05:41 +00:00
|
|
|
rb.AddForce(transform.up * jumpSpeed * 500 * Time.deltaTime, ForceMode.Impulse);
|
2022-01-29 09:57:53 +00:00
|
|
|
isGrounded = false;
|
2022-01-28 19:00:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 20:05:41 +00:00
|
|
|
rb.MovePosition(rb.position + moveDirection);
|
2022-01-28 19:00:56 +00:00
|
|
|
}
|
2022-01-29 09:57:53 +00:00
|
|
|
void OnCollisionExit(Collision other)
|
|
|
|
{
|
|
|
|
if (other.gameObject.tag == "platform")
|
|
|
|
{
|
|
|
|
Physics.gravity = -Vector3.up * 9.81f;
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 20:50:13 +00:00
|
|
|
|
2022-01-29 09:57:53 +00:00
|
|
|
void OnCollisionStay(Collision other){
|
|
|
|
if (other.gameObject.tag != "platform") return;
|
|
|
|
PlatformManager platform = other.gameObject.GetComponent<PlatformManager>();
|
|
|
|
if (platform == null)
|
|
|
|
{
|
|
|
|
// FIXME: Should platforms be allowed to not to have a PlatformManager?
|
|
|
|
// Debug.Log("ERROR");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Vector3 axis;
|
|
|
|
float angle;
|
|
|
|
axis = Vector3.Cross(-transform.up, -other.GetContact(0).normal);
|
|
|
|
if (platform.type == PlatformManager.PlatformType.SpeedUp)
|
|
|
|
{
|
|
|
|
angle = Mathf.Atan2(Vector3.Magnitude(axis), Vector3.Dot(-transform.up, -other.GetContact(0).normal));
|
|
|
|
transform.RotateAround(axis, angle);
|
|
|
|
// TODO: Rotate gravity to the center of platform
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnCollisionEnter(Collision other)
|
2022-01-28 20:50:13 +00:00
|
|
|
{
|
2022-01-29 09:57:53 +00:00
|
|
|
isGrounded = true;
|
2022-01-28 22:38:33 +00:00
|
|
|
if (other.gameObject.tag == "platform")
|
2022-01-28 20:50:13 +00:00
|
|
|
{
|
2022-01-29 00:39:17 +00:00
|
|
|
Vector3 axis;
|
2022-01-28 23:40:14 +00:00
|
|
|
float angle;
|
|
|
|
|
2022-01-29 02:15:33 +00:00
|
|
|
axis = Vector3.Cross(-transform.up, -other.GetContact(0).normal);
|
2022-01-28 23:40:14 +00:00
|
|
|
if (axis != Vector3.zero)
|
|
|
|
{
|
2022-01-29 02:15:33 +00:00
|
|
|
angle = Mathf.Atan2(Vector3.Magnitude(axis), Vector3.Dot(-transform.up, -other.GetContact(0).normal));
|
2022-01-28 23:40:14 +00:00
|
|
|
transform.RotateAround(axis, angle);
|
|
|
|
}
|
2022-01-29 02:15:33 +00:00
|
|
|
|
2022-01-29 00:19:21 +00:00
|
|
|
Vector3 gDirection;
|
|
|
|
PlatformManager platform = other.gameObject.GetComponent<PlatformManager>();
|
2022-01-29 09:57:53 +00:00
|
|
|
gDirection = -transform.up;
|
|
|
|
if (platform == null){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (platform.type == PlatformManager.PlatformType.Pull)
|
2022-01-29 00:39:17 +00:00
|
|
|
{
|
2022-01-29 02:15:33 +00:00
|
|
|
gDirection = -transform.up;
|
2022-01-29 00:39:17 +00:00
|
|
|
}
|
2022-01-29 09:57:53 +00:00
|
|
|
else if (platform.type == PlatformManager.PlatformType.Push)
|
2022-01-29 00:39:17 +00:00
|
|
|
{
|
2022-01-29 02:15:33 +00:00
|
|
|
gDirection = transform.up;
|
|
|
|
}
|
2022-01-29 09:57:53 +00:00
|
|
|
else if ((platform.type == PlatformManager.PlatformType.RotateY || platform.type == PlatformManager.PlatformType.RotateZ))
|
2022-01-29 02:15:33 +00:00
|
|
|
{
|
|
|
|
gDirection = -transform.up;
|
2022-01-29 00:39:17 +00:00
|
|
|
}
|
2022-01-29 09:57:53 +00:00
|
|
|
else if (platform.type == PlatformManager.PlatformType.SpeedUp)
|
2022-01-29 02:25:16 +00:00
|
|
|
{
|
2022-01-29 09:57:53 +00:00
|
|
|
rb.AddForce(other.transform.forward * platform.speed * Time.deltaTime, ForceMode.Impulse);
|
2022-01-29 02:25:16 +00:00
|
|
|
gDirection = -transform.up;
|
|
|
|
}
|
2022-01-29 09:57:53 +00:00
|
|
|
else if (platform.type == PlatformManager.PlatformType.SpeedDown)
|
2022-01-29 00:39:17 +00:00
|
|
|
{
|
2022-01-29 09:57:53 +00:00
|
|
|
rb.AddForce(other.transform.forward * platform.speed * 10 * Time.deltaTime, ForceMode.Impulse);
|
2022-01-29 02:15:33 +00:00
|
|
|
gDirection = -transform.up;
|
2022-01-29 00:19:21 +00:00
|
|
|
}
|
2022-01-28 21:51:06 +00:00
|
|
|
|
2022-01-28 22:18:07 +00:00
|
|
|
Physics.gravity = gDirection * 9.81f;
|
2022-01-28 20:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-29 09:57:53 +00:00
|
|
|
|
2022-01-28 19:00:56 +00:00
|
|
|
}
|