using System.Collections; using System.Collections.Generic; using UnityEngine; public class cs02 : MonoBehaviour { float speedX=0, speedY=0, speedZ=0; void Update() { //判別按下滑鼠左鍵 if (Input.GetMouseButtonDown(0)) { //(1)射線取得點按位置的坐標 Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit1; //射線檢測 if (Physics.Raycast(ray1, out hit1) == true) { //射線觸碰點的物件坐標 = hit1.point //agent1.SetDestination(hit1.point); //觸碰點的物件名稱 = hit1.collider.gameObject.name Debug.Log("hit object name=" + hit1.collider.gameObject.name); string name1 = hit1.collider.gameObject.name; if(name1== "FantasyBee") { //2秒後執行ZeroMoveY()函數,讓向上速度speedY=0 speedY = 1.0f; Invoke("StopMoveY", 1); } } }//endi if 判別滑鼠左鍵 //(2)幀動畫 float dx = speedX * Time.deltaTime; float dy = speedY * Time.deltaTime; float dz = speedZ * Time.deltaTime; this.gameObject.transform.Translate(dx, dy, dz, Space.Self); } void StopMoveY() { speedY = 0.0f; } }