- Home /
Bullet speed changes depending on how close and far I click the mouse button
Hi All, First of all I would like to apologize for my bad English, please bear with it.
So I have been working on a 2D Top Down space shooting game, where my space ship looks and moves in the direction of my mouse position. I somehow manage to work it out, but I'm facing some problems with it. When I shoot with my mouse away from my space ship the bullet goes faster, on the other hand when I shoot with my mouse very close to my space ship the bullet travels very slowly Here is my script for shooting
void Shoot () {
Vector3 shootDirection;
shootDirection = Input.mousePosition;
shootDirection.z = 0.0f;
shootDirection = Camera.main.ScreenToWorldPoint (shootDirection);
shootDirection = shootDirection - transform.position;
Rigidbody2D bulletInstance = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation) as Rigidbody2D;
bulletInstance.velocity = new Vector2 (shootDirection.x * bulletSpeed, shootDirection.y * bulletSpeed);
}
Answer by Rockyxx5 · Jan 15, 2016 at 05:43 PM
A very BIG thanks to OG23... The issue is solved. Here is the final script which did the job
void Shoot () {
Vector3 shootDirection;
shootDirection = Input.mousePosition;
shootDirection.z = 0.0f;
shootDirection = Camera.main.ScreenToWorldPoint (shootDirection);
shootDirection = shootDirection - transform.position;
//shootDirection = shootDirection.normalized;
Rigidbody2D bulletInstance = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation) as Rigidbody2D;
bulletInstance.velocity = new Vector2 (shootDirection.x, shootDirection.y).normalized * bulletSpeed;
}
Answer by hulahoolgames · Jan 13, 2016 at 07:43 PM
The problem is you are not normalizing the shootDirection after calculating. So the magnitude of the shootDirection vector depends on the distance between transform.position and the world mousePosition if you dont normalize it. Try this:
void Shoot () {
Vector3 shootDirection;
shootDirection = Input.mousePosition;
shootDirection.z = 0.0f;
shootDirection = Camera.main.ScreenToWorldPoint (shootDirection);
shootDirection = shootDirection - transform.position;
// normalize it so that no matter how far you have clicked the magnitude does not depend on the distance
shootDirection = shootDirection.normalized;
Rigidbody2D bulletInstance = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation) as Rigidbody2D;
bulletInstance.velocity = new Vector2 (shootDirection.x * bulletSpeed, shootDirection.y * bulletSpeed);
}
Thanks man, I owe you one. Normalizing the velocity did the job for me, however the code din't worked like you posted but the logic helped me figure it out. Here is the script I used to solve it
void Shoot () {
Vector3 shootDirection;
shootDirection = Input.mousePosition;
shootDirection.z = 0.0f;
shootDirection = Camera.main.ScreenToWorldPoint (shootDirection);
shootDirection = shootDirection - transform.position;
Rigidbody2D bulletInstance = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation) as Rigidbody2D;
bulletInstance.velocity = new Vector2 (shootDirection.x, shootDirection.y).normalized * bulletSpeed;
}
Your answer
Follow this Question
Related Questions
Unity 2D Shoot towards mouse position 1 Answer
shooting raycast 1 Answer
How to make my space ship shooting (2D Game)? 0 Answers
How to make 2d shooting towards the mouse but have it continue in staright line afterwards? 1 Answer
How can I get my bullet to shoot the other way when I face left. 2d 1 Answer