Unity 刚体移动,自方向移动

 场景

发射点

代码

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;

 效果

 

 

 都是朝向目标点发射移动的

原文地址:https://www.cnblogs.com/suiyi78/p/13512314.html