So I’m making a script that shoots gameobjects in the direction of the mouse cursor but I’m having a problem. I normalize the direction but the vector changes based on the distance from my character and the mouse position. Here’s the code:
void Update ()
{
// Save the current mouseposition.
mousePos = Input.mousePosition;
// Convert the mouseposition to a position in the game world.
mousePos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 20f));
direction = Vector3.Normalize( mousePos - transform.position);
if (Input.GetMouseButton(0))
{
Rigidbody bulletInstance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
bulletInstance.AddForce(direction * bulletspeed);
}
}
This of course means that the speed of the bullets differ based on the distance to the cursor. How do I fix this or rather, what am I doing wrong? Thanks.