I’m trying to figure out how to get a projectile to translate in a forward motion from the players gun to the mouse position clicked. Ive managed to find how to get the mouse position. But every time I replace translate(vector3.forward) to translate(mousePosition) the gun stops shooting anything
public class Bullet : MonoBehaviour
{
public float speed;
public float maxDistance;
private Camera RefMainCamera;
public Vector3 MousePos;
private Plane ZeroYPlane;
private void Start()
{
RefMainCamera = Camera.main;
ZeroYPlane = new Plane(Vector3.up, Vector3.zero);
}
// Update is called once per frame
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float _hitDistance;
//cast ray using zerpYplane to find out mouse position the ray intersects it
ZeroYPlane.Raycast(ray, out _hitDistance);
Vector3 MousePosition = ray.GetPoint(_hitDistance);
transform.Translate(Vector3.forward * Time.deltaTime * speed);
maxDistance += 1 * Time.deltaTime;
if(maxDistance >= 5)
{
Destroy(this.gameObject);
}
}
}