2022-01-28 19:00:56 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
|
|
{
|
2022-01-29 21:45:27 +00:00
|
|
|
public float maxDistanceFromCenterLine = 30.0f;
|
2022-01-29 16:23:26 +00:00
|
|
|
[Header("Move")]
|
2022-01-29 15:44:40 +00:00
|
|
|
public float speed = 7.5f;
|
|
|
|
public float maxSpeed = 15.0f;
|
|
|
|
public float minSpeed = 5.0f;
|
|
|
|
[HideInInspector]
|
|
|
|
public float currentSpeed = 0f;
|
|
|
|
private float modifier = 0.0f;
|
2022-01-29 16:23:26 +00:00
|
|
|
Vector3 moveDirection = Vector3.zero;
|
2022-01-29 18:26:09 +00:00
|
|
|
bool canMove = false;
|
2022-01-29 16:23:26 +00:00
|
|
|
Rigidbody rb;
|
|
|
|
float moveDirectionY;
|
|
|
|
public bool isRunning = false;
|
2022-01-29 20:22:03 +00:00
|
|
|
|
|
|
|
public bool isFalling = false;
|
2022-01-29 16:23:26 +00:00
|
|
|
[Header("Jump")]
|
2022-01-28 19:00:56 +00:00
|
|
|
public float jumpSpeed = 7.5f;
|
2022-01-29 16:23:26 +00:00
|
|
|
[HideInInspector]
|
|
|
|
public bool jump = false;
|
|
|
|
private bool isGrounded = false;
|
|
|
|
[Header("Camera")]
|
2022-01-28 19:00:56 +00:00
|
|
|
public float lookSpeed = 7.5f;
|
2022-01-29 14:40:02 +00:00
|
|
|
public float lookXLimit = 40.0f;
|
2022-01-28 19:00:56 +00:00
|
|
|
public Camera playerCamera;
|
|
|
|
public float rotationX = 0;
|
2022-01-29 16:23:26 +00:00
|
|
|
[Header("Others")]
|
|
|
|
public ParticleSystem runningParticles;
|
2022-01-29 02:15:33 +00:00
|
|
|
public Transform mainObject;
|
2022-01-29 21:54:12 +00:00
|
|
|
public AudioSource audioSource;
|
|
|
|
public List<AudioClip> audioClips = new List<AudioClip>();
|
2022-01-28 19:00:56 +00:00
|
|
|
// Start is called before the first frame update
|
|
|
|
|
2022-01-29 13:07:27 +00:00
|
|
|
private Vector3 saveDirection;
|
2022-01-29 11:03:01 +00:00
|
|
|
private Vector3 downDirection;
|
2022-01-29 15:44:40 +00:00
|
|
|
private Vector3 platformForward;
|
2022-01-29 11:03:01 +00:00
|
|
|
|
2022-01-29 02:15:33 +00:00
|
|
|
private Vector3 follow = Vector3.zero;
|
|
|
|
|
2022-01-29 20:03:18 +00:00
|
|
|
private GameObject pullObject = null;
|
|
|
|
private GameObject pushObject = null;
|
|
|
|
|
2022-01-28 19:00:56 +00:00
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
rb = GetComponent<Rigidbody>();
|
2022-01-29 15:44:40 +00:00
|
|
|
platformForward = Vector3.forward;
|
2022-01-29 11:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void disableCursor()
|
|
|
|
{
|
2022-01-28 19:00:56 +00:00
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
Cursor.visible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-29 18:26:09 +00:00
|
|
|
if (canMove && currentSpeed < minSpeed)
|
2022-01-29 15:44:40 +00:00
|
|
|
{
|
|
|
|
currentSpeed += 0.0005f;
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:30:18 +00:00
|
|
|
if (Input.GetAxis("Vertical") > 0 && currentSpeed < maxSpeed)
|
|
|
|
{
|
|
|
|
if (!canMove)
|
|
|
|
{
|
2022-01-29 18:26:09 +00:00
|
|
|
canMove = true;
|
|
|
|
}
|
2022-01-29 15:44:40 +00:00
|
|
|
currentSpeed += 0.01f;
|
2022-01-29 19:30:18 +00:00
|
|
|
}
|
|
|
|
else if (Input.GetAxis("Vertical") < 0 && currentSpeed > minSpeed)
|
|
|
|
{
|
2022-01-29 15:44:40 +00:00
|
|
|
currentSpeed -= 0.01f;
|
|
|
|
}
|
|
|
|
|
|
|
|
float curSpeedX = canMove ? (currentSpeed + modifier) : 0;
|
2022-01-29 20:45:56 +00:00
|
|
|
float curSpeedY = canMove ? (currentSpeed + 3.5f + modifier) * Input.GetAxis("Horizontal") : 0;
|
2022-01-28 19:00:56 +00:00
|
|
|
|
2022-01-29 15:44:40 +00:00
|
|
|
moveDirection = (platformForward * curSpeedX * Time.deltaTime) + (transform.right * curSpeedY * Time.deltaTime);
|
2022-01-28 19:00:56 +00:00
|
|
|
|
2022-01-29 17:03:31 +00:00
|
|
|
if ((currentSpeed + modifier) >= maxSpeed && !runningParticles.isPlaying)
|
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 17:03:31 +00:00
|
|
|
else if ((currentSpeed + modifier) < maxSpeed && runningParticles.isPlaying)
|
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
|
|
|
{
|
2022-01-29 12:08:41 +00:00
|
|
|
jump = true;
|
2022-01-29 09:57:53 +00:00
|
|
|
isGrounded = false;
|
2022-01-28 19:00:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 21:54:12 +00:00
|
|
|
if (audioSource != null && audioClips.Count > 0) {
|
|
|
|
if ((currentSpeed + modifier) > speed * 3f && (currentSpeed + modifier) < speed * 7f)
|
|
|
|
{
|
|
|
|
if (audioClips.Count > 1 && audioSource.clip != audioClips[1]) {
|
|
|
|
audioSource.Stop();
|
|
|
|
audioSource.clip = audioClips[1];
|
|
|
|
audioSource.Play();
|
|
|
|
}
|
|
|
|
} else if ((currentSpeed + modifier) <= speed * 3f) {
|
|
|
|
if (audioSource.clip != audioClips[0]) {
|
|
|
|
audioSource.Stop();
|
|
|
|
audioSource.clip = audioClips[0];
|
|
|
|
audioSource.Play();
|
|
|
|
}
|
|
|
|
} else if ((currentSpeed + modifier) >= speed * 7f) {
|
|
|
|
if (audioClips.Count > 2 && audioSource.clip != audioClips[2])
|
|
|
|
{
|
|
|
|
audioSource.Stop();
|
|
|
|
audioSource.clip = audioClips[2];
|
|
|
|
audioSource.Play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 20:05:41 +00:00
|
|
|
rb.MovePosition(rb.position + moveDirection);
|
2022-01-28 19:00:56 +00:00
|
|
|
}
|
2022-01-29 12:08:41 +00:00
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
|
|
|
if (jump)
|
|
|
|
{
|
2022-01-29 20:46:46 +00:00
|
|
|
// Debug.Log("Jump");
|
2022-01-29 12:08:41 +00:00
|
|
|
rb.AddForce(transform.up * jumpSpeed * 100 * Time.deltaTime, ForceMode.Impulse);
|
|
|
|
jump = false;
|
|
|
|
}
|
2022-01-29 13:07:27 +00:00
|
|
|
|
2022-01-29 21:14:15 +00:00
|
|
|
if (rb.velocity.magnitude != 0 && Vector3.Dot(rb.velocity.normalized, Physics.gravity.normalized) > 0)
|
2022-01-29 20:29:04 +00:00
|
|
|
{
|
2022-01-29 21:14:15 +00:00
|
|
|
// Debug.Log("Player is falling :)");
|
2022-01-29 20:29:04 +00:00
|
|
|
this.isFalling = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.isFalling = false;
|
|
|
|
}
|
|
|
|
|
2022-01-29 13:07:27 +00:00
|
|
|
if (saveDirection != Vector3.zero)
|
|
|
|
{
|
|
|
|
Vector3 axis;
|
|
|
|
float angle;
|
|
|
|
axis = Vector3.Cross(-transform.up, saveDirection);
|
|
|
|
|
|
|
|
angle = Mathf.Atan2(Vector3.Magnitude(axis), Vector3.Dot(-transform.up, saveDirection));
|
|
|
|
transform.RotateAround(axis, angle * Time.deltaTime * 5f);
|
|
|
|
}
|
2022-01-29 20:03:18 +00:00
|
|
|
|
|
|
|
if (pullObject != null)
|
|
|
|
{
|
|
|
|
PlatformManager platform = pullObject.GetComponent<PlatformManager>();
|
2022-01-29 21:14:15 +00:00
|
|
|
if (platform != null)
|
|
|
|
{
|
2022-01-29 20:03:18 +00:00
|
|
|
float step = platform.speed * Time.deltaTime * 10f;
|
|
|
|
rb.AddForce((pullObject.transform.position - transform.position) * step, ForceMode.Force);
|
|
|
|
}
|
2022-01-29 21:07:57 +00:00
|
|
|
if (Vector3.Distance(pullObject.transform.position, transform.position) > 5f)
|
2022-01-29 20:03:18 +00:00
|
|
|
{
|
|
|
|
pullObject = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pushObject != null)
|
|
|
|
{
|
2022-01-29 20:46:46 +00:00
|
|
|
PlatformManager platform = pushObject.GetComponent<PlatformManager>();
|
2022-01-29 20:03:18 +00:00
|
|
|
if (platform != null)
|
|
|
|
{
|
|
|
|
float step = platform.speed * Time.deltaTime * 10f;
|
|
|
|
rb.AddForce(-(pushObject.transform.position - transform.position) * step, ForceMode.Force);
|
|
|
|
}
|
2022-01-29 21:07:57 +00:00
|
|
|
if (Vector3.Distance(pushObject.transform.position, transform.position) > 5f)
|
2022-01-29 20:03:18 +00:00
|
|
|
{
|
|
|
|
pushObject = null;
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 21:14:15 +00:00
|
|
|
var distanceFromYAxis = new Vector2(rb.position.x, rb.position.y).magnitude;
|
|
|
|
if (distanceFromYAxis > maxDistanceFromCenterLine)
|
|
|
|
{
|
|
|
|
Debug.Log("Player fell out of map.");
|
|
|
|
rb.velocity = Vector3.zero;
|
|
|
|
Physics.gravity = -Vector3.up * 9.81f;
|
|
|
|
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
|
|
|
|
}
|
2022-01-29 12:08:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 09:57:53 +00:00
|
|
|
void OnCollisionExit(Collision other)
|
|
|
|
{
|
|
|
|
if (other.gameObject.tag == "platform")
|
|
|
|
{
|
2022-01-29 11:03:01 +00:00
|
|
|
Physics.gravity = this.downDirection * 9.81f;
|
2022-01-29 09:57:53 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-28 20:50:13 +00:00
|
|
|
|
2022-01-29 13:56:30 +00:00
|
|
|
void OnCollisionStay(Collision other)
|
|
|
|
{
|
2022-01-29 09:57:53 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-01-29 11:03:01 +00:00
|
|
|
|
2022-01-29 14:08:02 +00:00
|
|
|
if (other.GetContact(0).normal == other.transform.forward
|
|
|
|
|| other.GetContact(0).normal == -other.transform.forward
|
|
|
|
|| (other.GetContact(0).normal != -other.transform.up
|
|
|
|
&& other.GetContact(0).normal != other.transform.up
|
|
|
|
&& other.GetContact(0).normal != other.transform.right
|
|
|
|
&& other.GetContact(0).normal != -other.transform.right)
|
|
|
|
)
|
2022-01-29 12:48:05 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-01-29 14:34:38 +00:00
|
|
|
this.downDirection = -other.GetContact(0).normal;
|
|
|
|
saveDirection = -other.GetContact(0).normal;
|
2022-01-29 13:07:27 +00:00
|
|
|
|
2022-01-29 11:03:01 +00:00
|
|
|
// TODO: Handle other PlatformTypes
|
2022-01-29 11:50:26 +00:00
|
|
|
Physics.gravity = -other.GetContact(0).normal * 9.81f;
|
2022-01-29 09:57:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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 12:48:05 +00:00
|
|
|
|
2022-01-29 14:08:02 +00:00
|
|
|
if (other.GetContact(0).normal == other.transform.forward
|
|
|
|
|| other.GetContact(0).normal == -other.transform.forward
|
|
|
|
|| (other.GetContact(0).normal != -other.transform.up
|
|
|
|
&& other.GetContact(0).normal != other.transform.up
|
|
|
|
&& other.GetContact(0).normal != other.transform.right
|
|
|
|
&& other.GetContact(0).normal != -other.transform.right)
|
|
|
|
)
|
2022-01-29 11:03:01 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-01-28 23:40:14 +00:00
|
|
|
|
2022-01-29 13:07:27 +00:00
|
|
|
saveDirection = -other.GetContact(0).normal;
|
2022-01-29 02:15:33 +00:00
|
|
|
|
2022-01-29 20:03:18 +00:00
|
|
|
Vector3 gDirection = -other.GetContact(0).normal;
|
2022-01-29 00:19:21 +00:00
|
|
|
PlatformManager platform = other.gameObject.GetComponent<PlatformManager>();
|
2022-01-29 21:14:15 +00:00
|
|
|
if (platform != null)
|
|
|
|
{
|
2022-01-29 16:06:48 +00:00
|
|
|
switch (platform.type)
|
|
|
|
{
|
|
|
|
case PlatformManager.PlatformType.Push:
|
2022-01-29 20:03:18 +00:00
|
|
|
pushObject = other.gameObject;
|
|
|
|
pullObject = null;
|
2022-01-29 16:06:48 +00:00
|
|
|
break;
|
|
|
|
case PlatformManager.PlatformType.Pull:
|
2022-01-29 20:03:18 +00:00
|
|
|
pullObject = other.gameObject;
|
|
|
|
pushObject = null;
|
2022-01-29 16:06:48 +00:00
|
|
|
break;
|
|
|
|
case PlatformManager.PlatformType.RotateY:
|
|
|
|
break;
|
|
|
|
case PlatformManager.PlatformType.RotateZ:
|
|
|
|
break;
|
|
|
|
case PlatformManager.PlatformType.SpeedUp:
|
|
|
|
modifier += platform.speed;
|
|
|
|
break;
|
|
|
|
case PlatformManager.PlatformType.SpeedDown:
|
2022-01-29 19:30:18 +00:00
|
|
|
if (modifier - platform.speed >= 0)
|
|
|
|
{
|
2022-01-29 16:06:48 +00:00
|
|
|
modifier -= platform.speed;
|
2022-01-29 19:30:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-29 16:06:48 +00:00
|
|
|
modifier = 0.0f;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-01-29 19:27:34 +00:00
|
|
|
gDirection = -other.GetContact(0).normal;
|
2022-01-29 16:06:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-01-29 00:19:21 +00:00
|
|
|
}
|
2022-01-29 19:30:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gDirection = -transform.up;
|
|
|
|
}
|
|
|
|
platformForward = other.transform.forward;
|
2022-01-29 11:03:01 +00:00
|
|
|
this.downDirection = gDirection;
|
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
|
|
|
}
|