So as the title states, my bullets continue to follow the mouse around after I soot instead of going in a straight line. So if I move the mouse left, the bullets curve left. This would be great for a homing missile, but I’m not there right now. How do I store the position of the mouse so the bullet only goes to the stored position and not the new one? Here’s my code so far to both calculate the mouse position and fire the bullet towards it.
Finding mouse Position Followed by firing towards mouse:
// this creates a horizontal plane passing through this object's center
var plane = Plane(transform.position, Vector3.up);
// create a ray from the mousePosition
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// plane.Raycast returns the distance from the ray start to the hit point
var distance: float;
if (plane.Raycast(ray, distance)){
// some point of the plane was hit - get its coordinates
hitPoint = ray.GetPoint(distance);
hitPoint.z = -19;
}
direction = hitPoint - transform.position;
// Firing bullet towards mouse
transform.Translate(direction * bulletSpeed * Time.deltaTime)