場景
發射點
代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trajectory : MonoBehaviour
{
public GameObject target;
public GameObject rotatepos;
public GameObject game;
public float time = 1;
private GameObject pos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
Debug.DrawLine(ray.origin, hitInfo.point);//scene視圖可看到 DrawLine(Vector3 origin,Vector3 end,Color col):衹有儅發生碰撞時,在Scene視圖才可以看到畫出的射綫。
this.transform.position = new Vector3(hitInfo.point.x, 0f, hitInfo.point.z);
}
Vector3 dir = target.transform.position - rotatepos.transform.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 rotation = Quaternion.Lerp(rotatepos.transform.rotation, lookRotation, 10).eulerAngles;
rotatepos.transform.rotation = Quaternion.Euler(0f, rotation.y, 0f);
time -= Time.deltaTime;
if (time<0)
{
time = 0.25f;
pos= Instantiate(game);
pos.transform.position = this.transform.position;
pos.transform.eulerAngles = rotatepos.transform.eulerAngles;
print(rotatepos.transform.eulerAngles);
}
}
}
子彈屬性
剛體的移動 方向,是通過 獲取移動對象來判斷移動方向的
手動創建的值,是世界方向
body.velocity = new Vector3(50,0,0);
獲取對象的值在加值,可以選擇物體方向還是世界方向
body.velocity = this.transform.forward * 50;
效果
都是朝向目標點發射移動的