diff --git a/Assets/Scripts/CarriedCrateController.cs b/Assets/Scripts/CarriedCrateController.cs index b1eea52..bde69e6 100644 --- a/Assets/Scripts/CarriedCrateController.cs +++ b/Assets/Scripts/CarriedCrateController.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using Unity.VisualScripting; using UnityEngine; public class CarriedCrateController : MonoBehaviour @@ -17,4 +18,11 @@ void Update() { } + void OnJointBreak(float breakForce) + { + // 需要知道所在的player + Debug.Log("Crate is broken"); + var root = this.GetComponent().connectedBody.transform.parent.root.GetComponent(); + root.OnPlayerJointBreak(breakForce); + } } diff --git a/Assets/Scripts/GameMode.cs b/Assets/Scripts/GameMode.cs index 33d9931..09496fc 100644 --- a/Assets/Scripts/GameMode.cs +++ b/Assets/Scripts/GameMode.cs @@ -95,10 +95,9 @@ public void StopGame() Debug.Log("EndGame"); // 鎺у埗 UI Panel - uiController.EnterStartPanel(); + uiController.EnterEndPanel(); player.gameObject.SetActive(false); - //player GameObject.Destroy(player.gameObject); player2.gameObject.SetActive(false); GameObject.Destroy(player2.gameObject); diff --git a/Assets/Scripts/GrabSpawner.cs b/Assets/Scripts/GrabSpawner.cs index 272e34e..e171005 100644 --- a/Assets/Scripts/GrabSpawner.cs +++ b/Assets/Scripts/GrabSpawner.cs @@ -120,7 +120,7 @@ private void scare(PlayerController from, PlayerController to) { var dir = to.transform.position - from.transform.position; dir = dir.normalized; - to.rigidBody.AddForce(dir * to.horizontalSpeed * 200, ForceMode.Force); + to.rigidBody.AddForce(dir * to.rigidBody.mass * 200, ForceMode.Force); Debug.Log("玩家"+from.name+"让"+to.name+"远离自己"); } // 让对手靠近自己 @@ -128,7 +128,7 @@ private void attract(PlayerController from, PlayerController to) { var dir = from.transform.position - to.transform.position; dir = dir.normalized; - to.rigidBody.AddForce(dir * to.horizontalSpeed * 200, ForceMode.Force); + to.rigidBody.AddForce(dir * to.rigidBody.mass * 200, ForceMode.Force); Debug.Log("玩家" + from.name + "让" + to.name + "靠近自己"); } // 给自己回一滴血 diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index f078cd1..d1eeac3 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -45,6 +45,11 @@ void Start() } } + void endGame() + { + GameObject.Find("GameMode").GetComponent().StopGame(); + } + void Update() { //GROUNDED @@ -62,14 +67,19 @@ void Update() grounded = false; animator.SetBool("gr", false); } + if (Input.GetKey(moveLeftKey) && grounded) { - rigidBody.AddForce(transform.right * -horizontalSpeed * rigidBody.mass, ForceMode.Force); + rigidBody.AddForce(transform.right * -horizontalSpeed * rigidBody.mass , ForceMode.Force); + //单靠力量移动会很缓慢,手感不好,所以在此基础上加一个位置移动 + transform.position += transform.right * -horizontalSpeed * Time.deltaTime; } else if (Input.GetKey(moveRightKey) && grounded) { rigidBody.AddForce(transform.right * horizontalSpeed * rigidBody.mass, ForceMode.Force); + transform.position += transform.right * horizontalSpeed * Time.deltaTime; } + animator.SetFloat("turn", -rigidBody.velocity.z); if (Input.GetKey(jump) && grounded) { @@ -120,7 +130,7 @@ public void RemoveCrate() // 如果没有箱子了,游戏结束 if (allCarriedCrates.Count == 0) { - GameObject.Find("UI").GetComponent().EnterGamePanel(); + endGame(); } } @@ -143,6 +153,12 @@ public void AddCrate() rigidBody.mass += newCrate.GetComponent().mass; } + public void OnPlayerJointBreak(float breakForce) + { + // 箱子断开,游戏结束 + endGame(); + } + private void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Crate")