- Home /
Bullet firing but prefab looking in the wrong direction
Hi, beginner here, I wanted to recreate a little tank game i played during childhood, but I have a problem with my bullets. When fired, they follow the good trajectory ( bullet's forward direction = blue arrow) but the prefab is facing 90° right. I've model the bullet in blender, modify it's orientation in blender then put it back in unity but it doesn't change anything. I've tried several things, the bullet spawn with the position/rotation of the turret and it follow this script :
Can't you just copy the rotation of the tank's barrel as it's the same rotation the bullet should be facing?
The bullet should spawn at the position of the turret and have its own rotation (bullet's rotation) as spawning rotation ins$$anonymous$$d of being a turret rotation.
hi, thx for relpying. I'm making the bullet spawn on an empty object sitting on the gun and i'm using Instantiate(bulletPrefab, transform.position, transform.rotation); to make it spawn. Is that not the good way ?
Thats okay but you need to set the transform.rotation as the desired rotation you want the bullet to have.
If the bullet goes in the correct direction with the transform.forward
, it means your model is incorrectly oriented.
If you drag & drop the bullet prefab in an empty scene, is the blue arrow (+Z axis) in the same direction as the bullet? If not you have to fix this in your modelling software.
Answer by millej23 · May 12, 2020 at 01:15 PM
Is your bullet a Prefab? I would make it one, make sure the directions are what you want, then instantiate it using something like the example:
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
public class ExampleClass : MonoBehaviour
{
// Instantiate a Prefab with an attached Missile script
public Missile projectile;
void Update()
{
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1"))
{
// Instantiate the projectile at the position and rotation of this transform
Missile clone = Instantiate(projectile, transform.position, transform.rotation);
// Set the missiles timeout destructor to 5
clone.timeoutDestructor = 5;
}
}
}
You may need to re-do your prefab and check the rotation of that. Or what I usually do is make an empty gameobject, like "gunBarrel" where the bullet shoots from. This way you can change the empty object rotation the way you like without affecting your player, and its easier to make changes to the bullet tranforms with code, etc, for me anyway. Hope that helps!