GGJ2022/Assets/Scripts/PlayerController.cs

342 lines
10 KiB
C#
Raw Normal View History

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;
public float speedModifier = 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;
[HideInInspector]
public Rigidbody rb;
[HideInInspector]
public CharacterController chc;
2022-01-29 16:23:26 +00:00
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;
public float jumpModifier = 0.0f;
[HideInInspector]
2022-01-29 23:08:55 +00:00
public bool inAir = false;
2022-01-29 16:23:26 +00:00
private bool isGrounded = false;
private float jumpTimer = 0.1f;
private float jumpTime = 0f;
2022-01-29 16:23:26 +00:00
[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;
2022-01-30 10:42:15 +00:00
public List<AudioClip> jumpClips = new List<AudioClip>();
2022-01-29 21:54:12 +00:00
public List<AudioClip> audioClips = new List<AudioClip>();
PlatformManager collidePlatform;
GameObject colliderObject;
public string platformStatus = "exit";
2022-02-02 21:58:46 +00:00
float fallingTimer = 0.5f;
float fallingTime = 0f;
2022-01-28 19:00:56 +00:00
// Start is called before the first frame update
[HideInInspector]
public Vector3 saveDirection;
2022-01-29 11:03:01 +00:00
private Vector3 downDirection;
Vector3 _velocity = Vector3.zero;
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>();
chc = GetComponent<CharacterController>();
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()
{
2022-01-30 11:48:10 +00:00
if (UiController.isInMenu) return;
2022-01-28 19:00:56 +00:00
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.1f;
2022-01-29 15:44:40 +00:00
}
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;
}
2022-01-30 11:52:53 +00:00
float curSpeedX = canMove ? (currentSpeed + speedModifier): 0;
float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;
2022-01-28 19:00:56 +00:00
moveDirection = (transform.forward * curSpeedX * Time.deltaTime) + (transform.right * curSpeedY * Time.deltaTime);
2022-01-28 19:00:56 +00:00
2022-01-29 23:08:55 +00:00
if ((currentSpeed + speedModifier) >= 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 23:08:55 +00:00
else if ((currentSpeed + speedModifier) < 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-30 11:48:10 +00:00
if (audioSource != null && audioClips.Count > 0)
{
2022-02-02 21:58:46 +00:00
if ((currentSpeed + speedModifier) > speed * 6f && (currentSpeed + speedModifier) < speed * 8f)
2022-01-29 21:54:12 +00:00
{
2022-01-30 11:48:10 +00:00
if (audioClips.Count > 1 && audioSource.clip != audioClips[1])
{
2022-01-29 21:54:12 +00:00
audioSource.Stop();
audioSource.clip = audioClips[1];
audioSource.Play();
}
2022-01-30 11:48:10 +00:00
}
2022-02-02 21:58:46 +00:00
else if ((currentSpeed + speedModifier) <= speed * 6f)
2022-01-30 11:48:10 +00:00
{
if (audioSource.clip != audioClips[0])
{
2022-01-29 21:54:12 +00:00
audioSource.Stop();
audioSource.clip = audioClips[0];
audioSource.Play();
}
2022-01-30 11:48:10 +00:00
}
2022-02-02 21:58:46 +00:00
else if ((currentSpeed + speedModifier) >= speed * 8f)
2022-01-30 11:48:10 +00:00
{
2022-01-29 21:54:12 +00:00
if (audioClips.Count > 2 && audioSource.clip != audioClips[2])
{
audioSource.Stop();
audioSource.clip = audioClips[2];
audioSource.Play();
}
}
}
if (saveDirection != -transform.up)
{
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 * 8f);
}
PlatformManager localP = null;
if (collidePlatform != null)
{
collidePlatform.Step();
collidePlatform.Action(this, platformStatus);
localP = collidePlatform;
if (platformStatus == "enter" || platformStatus == "stay")
{
platformStatus = "stay";
collidePlatform = null;
}
}
else
{
if (platformStatus == "stay")
{
platformStatus = "exit";
fallingTime = fallingTimer + Time.time;
if (localP != null)
{
localP.Action(this, platformStatus);
localP = null;
}
}
}
2022-02-02 21:58:46 +00:00
if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || fallingTime > Time.time))
{
//inAir = true;
if (audioSource != null && jumpClips.Count > 0)
{
audioSource.PlayOneShot(jumpClips[Random.Range(0, jumpClips.Count)]);
}
_velocity = Vector3.zero;
2022-02-02 21:58:46 +00:00
fallingTime = Time.time;
jumpTime = jumpTimer + Time.time;
isGrounded = false;
}
if (jumpTime <= Time.time)
{
if (!isGrounded)
{
_velocity += Physics.gravity / 50 * Time.deltaTime;
}
else
{
_velocity = Physics.gravity / 10 * Time.deltaTime;
}
}
else
{
_velocity += transform.up * (jumpSpeed + jumpModifier) / 5 * Time.deltaTime;
}
chc.Move(moveDirection + _velocity);
//rb.MovePosition(rb.position + moveDirection);
if (platformStatus == "exit")
{
2022-02-02 21:58:46 +00:00
isGrounded = false;
}
2022-01-28 19:00:56 +00:00
}
2022-01-29 12:08:41 +00:00
private void FixedUpdate()
{
downDirection = -transform.up;
if (Physics.gravity != (downDirection * 9.81f)) {
if (downDirection == Vector3.forward)
{
downDirection = Vector3.down;
}
Physics.gravity = downDirection * 9.81f;
}
/*if (inAir)
2022-01-29 12:08:41 +00:00
{
2022-01-29 20:46:46 +00:00
// Debug.Log("Jump");
2022-01-30 10:42:15 +00:00
if (audioSource != null && jumpClips.Count > 0)
{
audioSource.PlayOneShot(jumpClips[Random.Range(0, jumpClips.Count)]);
}
chc.Move(transform.up * jumpSpeed * 100f * Time.deltaTime);
//rb.AddForce(transform.up * jumpSpeed * 100f * Time.deltaTime, ForceMode.Impulse);
2022-01-29 23:08:55 +00:00
inAir = false;
}*/
2022-01-29 13:07:27 +00:00
2022-01-30 11:48:10 +00:00
if (Vector3.Distance(transform.position, new Vector3(0f, 0f, transform.position.z)) > 10f)
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;
}
var distanceFromYAxis = new Vector2(transform.position.x, transform.position.y).magnitude;
2022-01-30 12:23:10 +00:00
if (UiController.isInMenu == false && distanceFromYAxis > this.maxDistanceFromCenterLine)
2022-01-29 21:14:15 +00:00
{
Debug.Log("Player fell out of map.");
//rb.velocity = Vector3.zero;
2022-01-30 12:23:10 +00:00
UiController.isInMenu = true;
Physics.gravity = Vector3.down * 9.81f;
UiController.SaveGame();
2022-01-29 21:14:15 +00:00
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
}
2022-01-29 12:08:41 +00:00
}
/*void OnCollisionExit(Collision other)
2022-01-29 09:57:53 +00:00
{
if (other.gameObject.tag != "platform") return;
2022-01-30 11:15:49 +00:00
PlatformManager platform = other.gameObject.GetComponent<PlatformManager>();
2022-01-30 11:15:49 +00:00
if (platform == null) return;
platform.Step();
platform.Action(this, "exit");
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;
Vector3 contact = other.GetContact(other.contacts.Length - 1).normal;
if (contact != -other.transform.up && contact != other.transform.up) {
2022-01-29 09:57:53 +00:00
return;
}
2022-01-29 11:03:01 +00:00
saveDirection = -contact;
}
void OnCollisionEnter(Collision other)
{
Debug.Log("Collide :D");
isGrounded = true;
if (other.gameObject.tag != "platform") return;
PlatformManager platform = other.gameObject.GetComponent<PlatformManager>();
if (platform != null)
2022-01-29 12:48:05 +00:00
{
platform.Step();
platform.Action(this, "enter");
}
Vector3 contact = other.GetContact(other.contacts.Length - 1).normal;
if (contact != -other.transform.up &&
contact != other.transform.up) {
2022-01-29 12:48:05 +00:00
return;
}
2022-01-29 13:07:27 +00:00
saveDirection = -contact;
} */
2022-01-29 09:57:53 +00:00
void OnControllerColliderHit(ControllerColliderHit hit)
2022-01-28 20:50:13 +00:00
{
Vector3 contact = hit.normal;
if (contact.normalized != -hit.gameObject.transform.right.normalized &&
contact.normalized != hit.gameObject.transform.right.normalized &&
contact.normalized != hit.gameObject.transform.forward.normalized &&
contact.normalized != -hit.gameObject.transform.forward.normalized)
{
jumpModifier = 0.0f;
isGrounded = true;
}
if (platformStatus == "exit" || colliderObject != hit.gameObject) {
platformStatus = "enter";
}
if (hit.gameObject.tag != "platform") return;
colliderObject = hit.gameObject;
collidePlatform = hit.gameObject.GetComponent<PlatformManager>();
if (contact.normalized != -hit.gameObject.transform.up.normalized &&
contact.normalized != hit.gameObject.transform.up.normalized)
2022-01-28 20:50:13 +00:00
{
return;
2022-01-28 20:50:13 +00:00
}
saveDirection = -contact;
2022-01-28 20:50:13 +00:00
}
2022-01-29 09:57:53 +00:00
2022-01-28 19:00:56 +00:00
}