From bab441e9028d6040d3282a9b90d78ea3ffef2d10 Mon Sep 17 00:00:00 2001 From: nexovec Date: Sat, 29 Jan 2022 10:57:53 +0100 Subject: [PATCH] Work on player movement --- Assets/Scenes/Vasek.unity | 12 ++++++ Assets/Scripts/PlatformManager.cs | 2 +- Assets/Scripts/PlayerController.cs | 63 +++++++++++++++++++++++------- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/Assets/Scenes/Vasek.unity b/Assets/Scenes/Vasek.unity index 5e5f411..da9d01e 100644 --- a/Assets/Scenes/Vasek.unity +++ b/Assets/Scenes/Vasek.unity @@ -332,6 +332,14 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 327689121} m_Modifications: + - target: {fileID: 3339862426328537977, guid: 99c4e582c7358f346ab00dae4ae956f9, type: 3} + propertyPath: type + value: 4 + objectReference: {fileID: 0} + - target: {fileID: 3339862426328537977, guid: 99c4e582c7358f346ab00dae4ae956f9, type: 3} + propertyPath: speed + value: 500 + objectReference: {fileID: 0} - target: {fileID: 8768991388683709944, guid: 99c4e582c7358f346ab00dae4ae956f9, type: 3} propertyPath: m_RootOrder value: 7 @@ -389,6 +397,10 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 327689121} m_Modifications: + - target: {fileID: 3339862426328537977, guid: 99c4e582c7358f346ab00dae4ae956f9, type: 3} + propertyPath: type + value: 4 + objectReference: {fileID: 0} - target: {fileID: 8768991388683709944, guid: 99c4e582c7358f346ab00dae4ae956f9, type: 3} propertyPath: m_RootOrder value: 6 diff --git a/Assets/Scripts/PlatformManager.cs b/Assets/Scripts/PlatformManager.cs index cb8f401..e97b02e 100644 --- a/Assets/Scripts/PlatformManager.cs +++ b/Assets/Scripts/PlatformManager.cs @@ -4,7 +4,7 @@ using UnityEngine; public class PlatformManager : MonoBehaviour { - public enum PlatformType {Pull, Push, RotateZ, RotateY, Speed}; + public enum PlatformType {Pull, Push, RotateZ, RotateY, SpeedUp, SpeedDown}; public PlatformType type = PlatformType.Pull; public float speed = 5; diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 98acfd3..accc660 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -21,7 +21,7 @@ public class PlayerController : MonoBehaviour float moveDirectionY; public Vector3 jump; public float jumpForce = 2.0f; - private bool isGoundet = false; + private bool isGrounded = false; public bool isRunning = false; private Vector3 follow = Vector3.zero; @@ -57,19 +57,47 @@ public class PlayerController : MonoBehaviour runningParticles.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear); } - if (Input.GetKeyDown(KeyCode.Space) && isGoundet) + if (Input.GetKeyDown(KeyCode.Space) && isGrounded) { Debug.Log("Jump"); rb.AddForce(transform.up * jumpSpeed * 500 * Time.deltaTime, ForceMode.Impulse); - isGoundet = false; + isGrounded = false; } rb.MovePosition(rb.position + moveDirection); } - - void OnCollisionStay(Collision other) + void OnCollisionExit(Collision other) { - isGoundet = true; + if (other.gameObject.tag == "platform") + { + Physics.gravity = -Vector3.up * 9.81f; + } + } + + void OnCollisionStay(Collision other){ + if (other.gameObject.tag != "platform") return; + PlatformManager platform = other.gameObject.GetComponent(); + 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) + { + isGrounded = true; if (other.gameObject.tag == "platform") { Vector3 axis; @@ -84,30 +112,35 @@ public class PlayerController : MonoBehaviour Vector3 gDirection; PlatformManager platform = other.gameObject.GetComponent(); - - if (platform != null && platform.type == PlatformManager.PlatformType.Pull) + gDirection = -transform.up; + if (platform == null){ + return; + } + if (platform.type == PlatformManager.PlatformType.Pull) { gDirection = -transform.up; } - else if (platform != null && platform.type == PlatformManager.PlatformType.Push) + else if (platform.type == PlatformManager.PlatformType.Push) { gDirection = transform.up; } - else if (platform != null && (platform.type == PlatformManager.PlatformType.RotateY || platform.type == PlatformManager.PlatformType.RotateZ)) + else if ((platform.type == PlatformManager.PlatformType.RotateY || platform.type == PlatformManager.PlatformType.RotateZ)) { gDirection = -transform.up; } - else if (platform != null && platform.type == PlatformManager.PlatformType.Speed) + else if (platform.type == PlatformManager.PlatformType.SpeedUp) + { + rb.AddForce(other.transform.forward * platform.speed * Time.deltaTime, ForceMode.Impulse); + gDirection = -transform.up; + } + else if (platform.type == PlatformManager.PlatformType.SpeedDown) { rb.AddForce(other.transform.forward * platform.speed * 10 * Time.deltaTime, ForceMode.Impulse); gDirection = -transform.up; } - else - { - gDirection = -transform.up; - } Physics.gravity = gDirection * 9.81f; } } + }