Help| bullet shooting forward, but falling down to the ground

hey, i have a bullet shooting script, but i got a problem,
the bullet is instantiating, and moving forward but he in the end, falling down to the ground and this is a really mess up.
how can i do that the bullet will always move forward, and not falling down ?

Sounds like you’re using a rigidbody projectile, you could either use a greater instantiation velocity or use a raycast to detect if you hit an enemy.

i dont want to use raycast :
i still want to use rigidbody, but make i say forward, and not falling down.

Try disabling gravity and drag.

Use the rigidbody constraints to stop it from falling(Y axis?).

i have only gravity, where is the “drag”?

NOW, i can shoot, and the bullet will move forward, but it’s still falls down to the ground, even it’s take much longer until it’s fall.
but i still need to know how to make that ONLY MOVE FORWARD WITHOUT FALLING DOWN?

As said, on your bullet prefab, look for the rigidbody component on the inspector. Then uncheck the gravity box, to disable it and prevent your bullet from falling.

as i saied…, i did it but it’s still falling…

Is there any force added each update to have it fall as well as move forward?

var bullet = Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
				bullet.rigidbody.AddForce(transform.forward * 2000);
       //Declare as Transform;
	var bulletSpawn : Transform;
	//Declare as RigidBody
	var bullet : Rigidbody;
	
		
        //Inside function
	var dir : Vector3 = bulletSpawn.forward;
	var bulletInstant : Rigidbody = Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
        bulletInstant.AddForce(dir * 2000);

And untick useGravity on Rigidbody component.

From what I’ve read in some sources, utilizing this technique can actually give you unpredictable results though, because the calculations each frame or update could wind up putting your bullet in front of a target in one frame, then due to the speed/movement calcs be on the other side of the target on the next frame, resulting in never hitting the target even when it should.

Maybe someone with more experience than I can verify or clarify, but that’s my understanding.

how?
do you mean :

???

i getting an error when i shoot in this line:

Do you have a prefab of your bullet, or are you just instantiating the component through code? The easiest way to disable the gravity is to use a prefab for the bullet, make sure the prefab has a rigidbody attached to it with the gravity option disabled (this is done in the inspector). Then you just instantiate that prefab in code instead of individual components.

From the little bit of code there, my first guess as to why you have a null reference error is because your bulletSpawn is a transform variable that is being declared, but it is never actually instantiated as an object. When you attempt to access the bulletSpawn it throws an error then because it doesn’t technically exist as an object yet. If you instantiate a prefab object, you would then just access its transform.forward vector.

UP.

Assuming you have disabled “Use Gravity” in this case, your bullet should move forward only. Forward, however, is in relation to the transform of bulletSpawn. Make sure that bulletSpawn is not rotated downward by any degree at the time that bullet is instantiated.

fixed it by rotating seems like the direction it headed was down

1 Like

I just use raycasting, myself, for the reason you noted… but I know people suggest that changing Collision Detection to continuous will also prevent the scenario you’re describing. I hadn’t ever tried it though just because I saw some posts where people were having troubles still even with continuous collision detection, and I couldn’t tell if they were just doing it wrong or if it didn’t work all the time.

You have just quoted a post from 2012… Woohoo.