using System; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; using UnityEngine.UIElements; public class PlayToyController : MonoBehaviour { private float throwTime; private float xForce, yForce; Vector2 mouseStartPosition = new(0f, 0f); Vector2 mouseEndPosition = new(0f, 0f); // forceAdjust 用于调节力量参数,数字越大减力效果越高 public int forceAdjust = 100; // Start is called once before the first execution of Update after the MonoBehaviour is created //void Start() //{ // Physics.gravity = new Vector3(0, -9.81f, 0); //} // FixedUpdate is called once per frame void FixedUpdate() { if (PlayData.gameStatus == PlayData.GameStatus.inProgress) { DogCatchToy(); } else if (PlayData.gameStatus != PlayData.GameStatus.notStart) // 游戏结束状态 { ShowGameResult(); } } public void Throw(InputAction.CallbackContext context) { //Debug.Log(context.control.device); if (PlayData.gameStatus == PlayData.GameStatus.inProgress) { return; } // 按下鼠标 if (context.phase == InputActionPhase.Started) { Ray ray = Camera.main.ScreenPointToRay(Pointer.current.position.ReadValue()); if (Physics.Raycast(ray, out RaycastHit hit)) { Debug.Log($"Clicked on: {hit.collider.gameObject.tag}"); // 射线检测起始点击是否在飞盘上 if (hit.collider.gameObject.name == "toy") { PlayData.isMouseStartOnTarget = true; mouseStartPosition = Pointer.current.position.ReadValue(); } } } // 松开鼠标 if (context.phase == InputActionPhase.Canceled && PlayData.isMouseStartOnTarget) { mouseEndPosition = Pointer.current.position.ReadValue(); Debug.Log("Drag end time at: " + context.duration); throwTime = Convert.ToSingle(context.duration); xForce = (mouseEndPosition.x - mouseStartPosition.x)/throwTime/forceAdjust; yForce = (mouseEndPosition.y - mouseStartPosition.y)/throwTime/forceAdjust; Debug.Log("xForce is: " + xForce); Debug.Log("yForce is: " + yForce); if (yForce > 1 && yForce > xForce) // 确保是向前飞出飞盘 { addThrowForce(); } // 重置值 mouseStartPosition = Vector2.zero; mouseEndPosition = Vector2.zero; } } // 飞碟飞出后,给飞碟添加一个力 public void addThrowForce() { float verticalForce = 6; // 定义垂直方向力量 var toy = GameObject.Find("toy"); PlayData.throwStartPoision = toy.transform.position; // 上报飞盘其实位置 PlayData.gameStatus = PlayData.GameStatus.inProgress; // 游戏状态设置为进行中 Rigidbody rb = toy.GetComponent(); Vector3 force = new Vector3(xForce, verticalForce, yForce); rb.isKinematic = false; rb.AddForce(force, ForceMode.Impulse); PlayData.gameStatus = PlayData.GameStatus.inProgress; } // 飞碟飞出后,狗追逐飞碟 public void DogCatchToy() { var dog = GameObject.Find("dog"); var toy = GameObject.Find("toy"); DogProperty dogProperty = EnviromentSetting.puppies[0]; // 读取狗的数据 float turnSpeed = 90.0f; // 每秒最多旋转角度 //dog.transform.LookAt(fisbee.transform.position); if (toy.transform.position.y < 0.43 && PlayData.gameStatus == PlayData.GameStatus.inProgress) { PlayData.gameStatus = PlayData.GameStatus.finishFail; // 游戏状态设置为失败。狗依然在追逐飞盘,但是飞盘已经落地 Debug.Log("飞盘落地了"); } else if (dog != null && toy != null) { // 计算目标方向 Vector3 direction = toy.transform.position - dog.transform.position; direction.y = 0; // 忽略垂直方向的偏移 var targetPosition = toy.transform.position; targetPosition.y = 0.4f; Quaternion targetRotation = new(); // 创建目标旋转 if (direction != Vector3.zero) { targetRotation = Quaternion.LookRotation(direction); } // 平滑旋转到目标方向 dog.transform.rotation = Quaternion.RotateTowards(dog.transform.rotation, targetRotation, turnSpeed * Time.deltaTime); float dogSpeed = dogProperty.runSpeed/10; // 狗向飞盘移动 dog.transform.position = Vector3.MoveTowards(dog.transform.position, targetPosition, dogSpeed * Time.deltaTime); // 狗切换到跑步状态 Animator animator = dog.GetComponent(); if (animator.GetBool("runState") != true) { animator.SetBool("runState", true); } } else { Debug.LogError("Dog or toy object is not assigned!"); } } // 初始化 playground UI 的按键语言 public void UILanguageInit() { var root = GameObject.Find("PlaygroundUIDocument").GetComponent().rootVisualElement; var btnArea = root.Q("btnArea"); var confirmBtn = btnArea.Q