From 25bf968311490792fac08caa34600f0722a3d769 Mon Sep 17 00:00:00 2001 From: haitem Date: Sat, 29 Jan 2022 21:03:18 +0100 Subject: [PATCH] Edit pull and push effect --- Assets/Scripts/PlayerController.cs | 39 +++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 900c10f..03f0812 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -37,6 +37,9 @@ public class PlayerController : MonoBehaviour private Vector3 follow = Vector3.zero; + private GameObject pullObject = null; + private GameObject pushObject = null; + void Start() { rb = GetComponent(); @@ -113,6 +116,33 @@ public class PlayerController : MonoBehaviour angle = Mathf.Atan2(Vector3.Magnitude(axis), Vector3.Dot(-transform.up, saveDirection)); transform.RotateAround(axis, angle * Time.deltaTime * 5f); } + + if (pullObject != null) + { + PlatformManager platform = pullObject.GetComponent(); + if (platform != null) { + float step = platform.speed * Time.deltaTime * 10f; + rb.AddForce((pullObject.transform.position - transform.position) * step, ForceMode.Force); + } + if (Vector3.Distance(pullObject.transform.position, transform.position) > 10f) + { + pullObject = null; + } + } + + if (pushObject != null) + { + PlatformManager platform = pullObject.GetComponent(); + if (platform != null) + { + float step = platform.speed * Time.deltaTime * 10f; + rb.AddForce(-(pushObject.transform.position - transform.position) * step, ForceMode.Force); + } + if (Vector3.Distance(pushObject.transform.position, transform.position) > 10f) + { + pushObject = null; + } + } } void OnCollisionExit(Collision other) @@ -171,17 +201,18 @@ public class PlayerController : MonoBehaviour saveDirection = -other.GetContact(0).normal; - Vector3 gDirection; + Vector3 gDirection = -other.GetContact(0).normal; PlatformManager platform = other.gameObject.GetComponent(); - gDirection = -other.GetContact(0).normal; if (platform != null) { switch (platform.type) { case PlatformManager.PlatformType.Push: - rb.AddForce(other.GetContact(0).normal * platform.speed * Time.deltaTime, ForceMode.Impulse); + pushObject = other.gameObject; + pullObject = null; break; case PlatformManager.PlatformType.Pull: - rb.AddForce(-other.GetContact(0).normal * platform.speed * Time.deltaTime, ForceMode.Impulse); + pullObject = other.gameObject; + pushObject = null; break; case PlatformManager.PlatformType.RotateY: break;