From 11a517ba609a794e5b2fe58b793060df8cc76277 Mon Sep 17 00:00:00 2001 From: nexovec Date: Sat, 29 Jan 2022 12:03:01 +0100 Subject: [PATCH] Fixed weird collision angles. --- .../Materials/PlayerMaterial.physicMaterial | 14 +++++++ .../PlayerMaterial.physicMaterial.meta | 8 ++++ Assets/Scenes/Vasek.unity | 42 +++++++++++++++++++ Assets/Scripts/PlayerController.cs | 36 +++++++++++++++- 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 Assets/Materials/PlayerMaterial.physicMaterial create mode 100644 Assets/Materials/PlayerMaterial.physicMaterial.meta diff --git a/Assets/Materials/PlayerMaterial.physicMaterial b/Assets/Materials/PlayerMaterial.physicMaterial new file mode 100644 index 0000000..519c956 --- /dev/null +++ b/Assets/Materials/PlayerMaterial.physicMaterial @@ -0,0 +1,14 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!134 &13400000 +PhysicMaterial: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: PlayerMaterial + dynamicFriction: 0.6 + staticFriction: 0.6 + bounciness: 0 + frictionCombine: 1 + bounceCombine: 0 diff --git a/Assets/Materials/PlayerMaterial.physicMaterial.meta b/Assets/Materials/PlayerMaterial.physicMaterial.meta new file mode 100644 index 0000000..ced15df --- /dev/null +++ b/Assets/Materials/PlayerMaterial.physicMaterial.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 055b414bc685d41409a41f4ea2d64231 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 13400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Vasek.unity b/Assets/Scenes/Vasek.unity index da9d01e..e03ed4f 100644 --- a/Assets/Scenes/Vasek.unity +++ b/Assets/Scenes/Vasek.unity @@ -972,6 +972,24 @@ Transform: m_CorrespondingSourceObject: {fileID: 8768991388683709944, guid: 99c4e582c7358f346ab00dae4ae956f9, type: 3} m_PrefabInstance: {fileID: 613611969} m_PrefabAsset: {fileID: 0} +--- !u!1 &1172583617 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 3244356566819001084, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} + m_PrefabInstance: {fileID: 1953646381} + m_PrefabAsset: {fileID: 0} +--- !u!65 &1172583624 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1172583617} + m_Material: {fileID: 13400000, guid: 055b414bc685d41409a41f4ea2d64231, type: 2} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 1, y: 2, z: 1} + m_Center: {x: 0, y: 0, z: 0} --- !u!4 &1210902573 stripped Transform: m_CorrespondingSourceObject: {fileID: 8768991388683709944, guid: 99c4e582c7358f346ab00dae4ae956f9, type: 3} @@ -1053,6 +1071,22 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 3244356566819001072, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} + propertyPath: m_Enabled + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3244356566819001073, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} + propertyPath: m_Enabled + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3244356566819001085, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} + propertyPath: m_LocalPosition.y + value: 0.11 + objectReference: {fileID: 0} + - target: {fileID: 3244356566819001087, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} + propertyPath: m_CollisionDetection + value: 2 + objectReference: {fileID: 0} - target: {fileID: 3244356567786994344, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} propertyPath: m_Name value: Player @@ -1101,6 +1135,14 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 3244356567913423212, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} + propertyPath: m_LocalPosition.y + value: 0.55 + objectReference: {fileID: 0} + - target: {fileID: 3244356567913423212, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} + propertyPath: m_LocalPosition.z + value: 0.5 + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: ec622cf6f0988bc42a99ba84304c15a9, type: 3} --- !u!4 &2009655149 stripped diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index accc660..688dc4f 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -24,6 +24,8 @@ public class PlayerController : MonoBehaviour private bool isGrounded = false; public bool isRunning = false; + private Vector3 downDirection; + private Vector3 follow = Vector3.zero; void Start() @@ -70,7 +72,7 @@ public class PlayerController : MonoBehaviour { if (other.gameObject.tag == "platform") { - Physics.gravity = -Vector3.up * 9.81f; + Physics.gravity = this.downDirection * 9.81f; } } @@ -86,12 +88,27 @@ public class PlayerController : MonoBehaviour Vector3 axis; float angle; axis = Vector3.Cross(-transform.up, -other.GetContact(0).normal); + + 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 + ) + ) + { + return; + } + Physics.gravity = this.downDirection * 9.81f; 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 } + // TODO: Handle other PlatformTypes + Physics.gravity = this.downDirection * 9.81f; } @@ -100,6 +117,18 @@ public class PlayerController : MonoBehaviour isGrounded = true; if (other.gameObject.tag == "platform") { + 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 + ) + ) + { + return; + } Vector3 axis; float angle; @@ -114,6 +143,8 @@ public class PlayerController : MonoBehaviour PlatformManager platform = other.gameObject.GetComponent(); gDirection = -transform.up; if (platform == null){ + // FIXME: remove + this.downDirection = -transform.up; return; } if (platform.type == PlatformManager.PlatformType.Pull) @@ -138,6 +169,7 @@ public class PlayerController : MonoBehaviour rb.AddForce(other.transform.forward * platform.speed * 10 * Time.deltaTime, ForceMode.Impulse); gDirection = -transform.up; } + this.downDirection = gDirection; Physics.gravity = gDirection * 9.81f; }