unity3d物體移動

物體按照某方向移動。
需求,子彈攻擊tPlayer。
tPlayer爲目標,使用重力的velocity屬性進行移動。

Vector3 direction = tPlayer.transform.position - transform.position;
            GetComponent<Rigidbody>().velocity = direction.normalized * speed;

方式二:

transform.LookAt (target.transform);				//敵人面向追蹤目標
transform.eulerAngles=new Vector3(0.0f,transform.eulerAngles.y,0.0f);	//設置敵人的Rotation屬性,確保敵人只在y軸旋轉
transform.position += transform.forward * moveSpeed * Time.deltaTime;	//敵人以moveSpeed的速度向追蹤目標靠近

上述兩者的區別,下者的前進方向爲水平樣式,上者的前進方向不是水平方向。
如果有問題,歡迎留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章