- Home /
My 2d spawned bullet prefabs will not fly up, just fall
So this ones weird, my bullets are spawning but when they do they just fall down. IT looks like it has to do with gravity and the rigidbodies, but here is my code anyway:
void Update () {
handleShooting();
handleMove();
handlePause();
}
void handlePause()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
cursorLock = !cursorLock;
}
if (cursorLock == true)
{
Cursor.visible = false;
Time.timeScale = 1;
} else
{
Cursor.visible = true;
Time.timeScale = 0;
}
}
void handleMove()
{
if (cursorLock == true)
{
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
}
}
void handleShooting()
{
if (canShoot == true)
{
shoot();
StartCoroutine(fire());
}
}
void shoot()
{
var bulletBullet = (GameObject)Instantiate(
bullet,
gun.position,
gun.rotation);
bullet.GetComponent<Rigidbody2D>().velocity = bullet.transform.forward * shootVelocity;
Destroy(bulletBullet, 1.2f);
}
any ideas?
Answer by tormentoarmagedoom · May 11, 2018 at 07:54 AM
Good day.
Can you post a screenshot of the inspector?
Check the Rigidbody compoenent. Unmark the "Use gravity" and mark "is Kinematic".
Bye
Okay, so this is whats weird, my rigidbody 2d doesnt have th option to not use gravity.
It seems the Editor is different for the Rigidbody2D
, ins$$anonymous$$d of bool useGravity
it has float gravityScale
(weird decision to have different interface, in my opinion). Anyway, just set gravityScale
to 0 to disable gravity.
I turned gravity off, but now it doesn't move at all. How can I get it to shoott up if theres no gravity?
Answer by Harinezumi · May 16, 2018 at 07:28 AM
Is the code still the same as what you posted originally? In that case, the problem is that you instantiate a bullet from the prefab ( bulletBullet
), but then assign the velocity to the prefab bullet
(I wonder how no one has noticed this so far):
void shoot () {
var bulletBullet = (GameObject)Instantiate(bullet, gun.position, gun.rotation);
bullet.GetComponent<Rigidbody2D>().velocity = bullet.transform.forward * shootVelocity; // <-- this will not affect your bullet!
}
There might be other problems, but this is the first step to make your bullet fly!
Also, you could store your bullet prefab by its Rigidbody2D
, and then instantiate as such, so that you don't need to get the component. Like this:
[SerializeField] private Rigidbody2D bullet = null; // variable for prefab to be assigned in Editor
...
void shoot () {
Rigidbody2D bulletInstance = Instantiate(bullet, gun.position, gun.rotation);
bulletInstance.velocity = bulletInstance.transform.forward * shootVelocity;
}
Answer by Kciwsolb · May 11, 2018 at 01:29 PM
Since you are working in 2D, you probably don't want to use transform.forward. That is the Z axis (blue arrow). In 2D, this axis points directly away from the camera. So it probably isn't doing what you want. And it may look like it's falling because it has gravity checked? Can't be sure without an inspector screenshot. But anyways, try using either transform.up or transform.right (depending on which way your bullet sprite faces) instead, and uncheck gravity if it is checked.
I assume this would work, but I cant test it because I'm being told the only way to stop the falling is to make the gravity zero, but then it cant move at all. Heres the editor if that helps:
Your answer
Follow this Question
Related Questions
Bullet always facing the sky. 3 Answers
Applying force to a rigidbody 2 Answers
Bullet hell - how to make a lot of bullets? 3 Answers
Horizontal gravity on One gameobject 2 Answers