using Cinemachine; using System; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; using UnityEngine.UIElements; /* PlayToyController玩飞盘的主要控制代码 * DogCatchToy 启动狗去抓玩具飞盘 * ShowGameResult 显示游戏结算canvas * Throw 丢飞盘出去控制 * addThrowForce 给飞盘玩具添加初始力量 * UILanguageInit 初始化游戏语言 * GameReset 游戏重置 * PlayData 静态类用于存储游戏运行状态和数据 */ 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 = 60; private CinemachineVirtualCamera CamPlayer, CamDog; private GameObject dog; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { PlayData.Reset(); CamPlayer = GameObject.Find("Player CAM").GetComponent(); CamDog = GameObject.Find("Dog CAM").GetComponent(); } // 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) { // 动态配置Dog V Cam。不能放在Start()里面,因为狗是动态加载的。Start时候狗还没有加载。 dog = GameObject.Find("dog"); CamDog.m_Follow = dog.transform; CamDog.m_LookAt = dog.transform; 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); float screenWidth = Screen.width; float screenHeight = Screen.height; float aspectRatio = screenWidth / screenHeight; Debug.Log("屏幕宽高比: " + aspectRatio); xForce = (mouseEndPosition.x - mouseStartPosition.x)/throwTime/forceAdjust*aspectRatio; yForce = (mouseEndPosition.y - mouseStartPosition.y)/throwTime/forceAdjust*aspectRatio; Debug.Log("xForce is: " + xForce); Debug.Log("yForce is: " + yForce); if (yForce > 1 && yForce > xForce*0.5f) // 确保是向前飞出飞盘 { AddThrowForce(); } // 重置值 mouseStartPosition = Vector2.zero; mouseEndPosition = Vector2.zero; } } // 飞碟飞出后,给飞碟添加一个力 public void AddThrowForce() { if (PlayData.gameStatus == PlayData.GameStatus.notStart) { 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; } else { return; } } // 飞碟飞出后,狗追逐飞碟 public void DogCatchToy() { // 调整摄像机 CamPlayer.Priority = 0; CamDog.Priority = 10; var dog = GameObject.Find("dog"); var toy = GameObject.Find("toy"); DogProperty dogProperty = UserProperty.dogs[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; // 游戏状态设置为失败。狗依然在追逐飞盘,但是飞盘已经落地 Animator animator = dog.GetComponent(); animator.SetBool("runState", false); 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.LookAt(targetPosition); // 平滑旋转到目标方向 //dog.transform.rotation = Quaternion.RotateTowards(dog.transform.rotation, targetRotation, turnSpeed * Time.deltaTime); float dogSpeed = (100+dogProperty.runSpeed)/20; // 狗向飞盘移动 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 UI_Initial() { var root = GameObject.Find("PlaygroundUIDocument").GetComponent().rootVisualElement; var btnArea = root.Q("btnArea"); var confirmBtn = btnArea.Q