using System.Collections; using System.Collections.Generic; using UnityEngine; //用add forece的方法,不好 //原因1:程式碼只能使用在單一物件(複合物件,每個子物件,都要放程式碼) //原因2:大部分的遊戲,都是用動力學kenematic模式,不是用力學模式(例如重力....) //所以:如果想要推一個物件2秒移動的方法(不建議用addForce函數,因為它必須使用rigidbody的力學組件) //this.gameObject.GetComponent().AddForce(Vector3.right*0.001f); public class cs01 : MonoBehaviour { float speedX, speedY, speedZ; private void OnMouseDown() { //2秒後執行ZeroMoveY()函數,讓向上速度speedY=0 speedY = 1.0f; Invoke("StopMoveY", 1); } void StopMoveY() { speedY = 0.0f; } void Update() { float dx = speedX * Time.deltaTime; float dy = speedY * Time.deltaTime; float dz = speedZ * Time.deltaTime; this.gameObject.transform.Translate(dx, dy, dz, Space.Self); } }