How to fix speed changes depending on mouse distance?
I am tinkering with shooting using your mouse to look around and shoot. Which I have gotten it to do so but the speed of the projectile changes depending on the distance of the spawn point of the projectile to the mouse. Am I looking right at the solution or is there a better way to just shoot the projectile in the direction that it is facing?
Part of my Script:
void Shoot() { Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y); RaycastHit2D Hit = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, toHit);
Debug.DrawLine(firePointPosition, mousePosition);
Vector3 bulletDirection = new Vector3();
GameObject Bullet = Instantiate(bullet, this.transform.position, Quaternion.identity) as GameObject;
Bullet.GetComponent<Rigidbody2D>().velocity = (mousePosition - firePointPosition);
}
Comment