So after I instantiate my bullets and fire towards my mouse in this sidescroller, the bullets continue to follow the mouse. If you move the mouse left, the bullets will curve left. The speed of the bullets will also be slower if you shoot close to the player, because the bullet wants to stay around the mouse. Here’s a short video so you can visualize my problem: - YouTube .
Now the code on my PLayerScript (To find location of the mouse and instantiate the bullet)
// 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;
if(Input.GetMouseButtonUp (0) )
{
var instantiatedbullet = Instantiate(bullet,GameObject.Find("SpawnPoint").transform.position, Quaternion.Euler(direction.normalized));
}
Then my code on my BulletScript to move it forwards after being instantiated.
//amount to move bullet
amtToMove = bulletSpeed * Time.deltaTime ;
transform.Translate(Vector3.fwd * amtToMove);
So how do I have the bullet remember the spot the cursor was at when the mouse button was clicked up, and only move to and then past that point?