GGJ2022/Assets/Scripts/PlayerController.cs

101 lines
3.5 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
{
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;
// 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-28 23:07:22 +00:00
private bool isGoundet = false;
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);
bool isRunning = Input.GetKey(KeyCode.LeftShift);
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 00:39:17 +00:00
if (isRunning && !myParticles.isPlaying())
{
runningParticles.Play();
}
else if (!isRunning && myParticles.isPlaying())
{
runningParticles.Stop();
}
2022-01-29 00:19:21 +00:00
if (Input.GetKeyUp(KeyCode.Space) && isGoundet)
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-28 23:07:22 +00:00
isGoundet = 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-28 20:50:13 +00:00
2022-01-28 22:38:33 +00:00
void OnCollisionEnter(Collision other)
2022-01-28 20:50:13 +00:00
{
2022-01-28 23:07:22 +00:00
isGoundet = true;
2022-01-28 22:38:33 +00:00
if (other.gameObject.tag == "platform")
2022-01-28 20:50:13 +00:00
{
2022-01-28 22:18:07 +00:00
var thisTransform = this.gameObject.transform;
2022-01-28 23:40:14 +00:00
2022-01-29 00:39:17 +00:00
Vector3 axis;
2022-01-28 23:40:14 +00:00
float angle;
axis = Vector3.Cross(-transform.up, -other.transform.up);
if (axis != Vector3.zero)
{
angle = Mathf.Atan2(Vector3.Magnitude(axis), Vector3.Dot(-transform.up, -other.transform.up));
transform.RotateAround(axis, angle);
}
2022-01-29 00:19:21 +00:00
Vector3 gDirection;
PlatformManager platform = other.gameObject.GetComponent<PlatformManager>();
2022-01-29 00:39:17 +00:00
if (platform != null && platform.type == PlatformManager.PlatformType.Pull)
{
2022-01-29 00:19:21 +00:00
gDirection = new Quaternion(0.0f, 0.0f, other.gameObject.transform.rotation.z, 1.0f) * Vector3.down;
2022-01-29 00:39:17 +00:00
}
else if (platform != null && platform.type == PlatformManager.PlatformType.Push)
{
2022-01-29 00:19:21 +00:00
gDirection = new Quaternion(0.0f, 0.0f, other.gameObject.transform.rotation.z, 1.0f) * Vector3.up;
2022-01-29 00:39:17 +00:00
}
else
{
2022-01-29 00:19:21 +00:00
gDirection = new Quaternion(0.0f, 0.0f, other.gameObject.transform.rotation.z, 1.0f) * Vector3.down;
}
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-28 19:00:56 +00:00
}