我已经制作了一个从我的摄像机到点击对象点的射线。然而,我正在尝试让一个物体(在这种情况下是一颗子弹)沿着射线路径飞行。目前它会因为向量3而直线向前从摄像机飞出,无论你在对象上的哪个位置点击。如何让它遵循射线路径?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RaycastShot : MonoBehaviour {
public Camera camera;
private Ray ray;
private RaycastHit hit;
public GameObject bullet;
private GameObject createBullet;
private Collider collider;
void Update () {
if (Input.GetMouseButtonDown (0)) {
ray = camera.ScreenPointToRay (Input.mousePosition);
createBullet = Instantiate (bullet, camera.transform.position, bullet.transform.rotation);
createBullet.AddComponent<Rigidbody>();
createBullet.GetComponent<Rigidbody>().AddRelativeForce (new Vector3(0, 1500, 0));
createBullet.GetComponent<Rigidbody>().useGravity = false;
collider = createBullet.GetComponent<Collider> ();
Destroy (collider);
if (Physics.Raycast (ray, out hit)) {
}
}
Debug.DrawLine (ray.origin, hit.point, Color.red);
}
}
createBullet.GetComponent<Rigidbody>().AddRelativeForce (ray.direction);
- Liam Smith