Spawn bullet prefab

Hey guys, what do you think is the best way to spawn a bullet prefab and and what is the function used in flax like the unity’s instantiate function

There is an example of how to do this in the Flax documentation:

The code snippet to spawn a prefab is:

PrefabManager.SpawnPrefab(myPrefab, new Vector3(0, 10, 0));

Also, this question belongs in the scripting section of the forums. Spawning prefabs has nothing to do with physics.

how do i add force to it, like bullet physics.
CODE EXAMPLE PLEASE

Here is an example of how to spawn a prefab in Visual Script:

In terms of force, just add a certain amount of position each frame and multiply that my deltaTime.

1 Like

Can you drop that code here, cos I’m trying to find out something

Sure. C# or VisualScript?

c# please it’s more comfortable for me

Okay, I’ve built a very simple bullet system. More info on spawning prefabs can be found here in the Documentation. The first thing you’ll need is a weapon or some actor to handle the spawning of bullets:

Next you’re going to need another empty actor that sist at the spot which you want to spawn your bullets. Make sure it’s rotation is set to 0.

Next, create a script and attach it to the weapon. The code should be like the following:

using System;
using System.Collections.Generic;
using FlaxEngine;

namespace Game
{
    /// <summary>
    /// NewWeapon Script.
    /// </summary>
    public class NewWeapon : Script
    {
        [Serialize] [ShowInEditor] Prefab BulletPrefab;
        [Serialize] [ShowInEditor] Actor SpawnPosition;

        public override void OnUpdate()
        {
            if (Input.GetAction("Fire"))
            {
            PrefabManager.SpawnPrefab(BulletPrefab, SpawnPosition.Position, 
            SpawnPosition.Transform.Orientation);
            }
        }
    }
}

If you do not have a Fire action, crate one and set it’s mode to Press:

Next, you’ll want to create a Bullet prefab. I just created a simple sphere and set its scale to 0.1. Now you’ll want to attach the actors to the references we made:

Now, we can add a script to the bullet:

using System;
using System.Collections.Generic;
using FlaxEngine;

namespace Game
{
    /// <summary>
    /// Bullet Script.
    /// </summary>
    public class Bullet : Script
    {
        // Speed can be tuned to your liking ;)
        [Serialize] [ShowInEditor] float Speed = 2000;

        /// <inheritdoc/>
        public override void OnUpdate()
        {
            Actor.Position += Transform.Forward * Speed * Time.DeltaTime;
        }
    }
}

Now, save the prefab and test your game. You should have a fully working bullet system! Of course, you’d also want to add collision detection, a despawn timer, and other things to your bullets. That can all be done in your bullet script. This is just the movement part.

1 Like

If I wanted to add collision, I just need to create a collider for my bullet prefab right? , also is it ok if I make my bullet prefab a rigidbody

Yes, you just need to add a collision shape. As for the rigidbody, that might cause some performance hitches, if you are spawning many bullets. If you do go the rigidbody route, make sure to set the rigidbody as the root Actor of the bullet and add the mesh and the collision shape as children of the bullet.