# How can you instantiate a prefab with an initial burst of speed using ForceMode2D.Impulse?

Hi Everyone,

As many people who post questions I am new. I’ll get straight to the point and say my problem arises when trying to use ForceMode2D.Impulse. Currently in my game I am trying to make a Prefab get launched with an initial force and then continue with a steady speed while targeting my player. Thanks to Brackeys I figured out how to replicate a 2D homing missile (How to make a Homing Missile in Unity - YouTube) which is basically how my script is working right now. Not only that, but I followed his other tutorial and used another sprite attached with a small script to instantiate it.

[RequireComponent(typeof(Rigidbody2D))]
public class Attack : MonoBehaviour
{
public Transform target;

``````public float flyspeed = 5f;
public float rotateSpeed = 200f;

public Rigidbody2D rb;

void Start()
{
rb = GetComponent<Rigidbody2D>();
}

void initialV()
{
}

void FixedUpdate()
{
initialV();

Vector2 direction = (Vector2)target.position - rb.position;

direction.Normalize();

float rotateAmount = Vector3.Cross(direction, transform.up).z;

rb.angularVelocity = -rotateAmount * rotateSpeed;

rb.velocity = transform.up * flyspeed;
}
``````

Above is the homing missile script (named Attack), but it also includes my efforts to use ForceMode2D.impulse to increase the speed at the start of each prefab clone’s instantiation.

``````public Transform firePoint;

//public Rigidbody2D rb;

public GameObject attackPrefab;

/*void Start()
{
rb = GetComponent<Rigidbody2D>();
}*/

void Shoot()
{
Instantiate(attackPrefab, firePoint.position, firePoint.rotation);
}

void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
Shoot();
}
}
``````
• Above is the instantiation script (named Weapon) that works well, but commented out are my efforts to try and instantiate something with a the ForceMode2D.Impulse.
• This photo shows that I used the Rigidbody of attackPrefab for the script attack. The script attack is attached to the attackPreFab(I’m currently using the texture of a coin for the attackPrefab).
• To avoid confusion I’ll say the things I have already tried and what I know about ForceMode2D.Impulse. To use this method on a game object it has to have a RigidBody2D and in unity you must attach the Rigidbody2D to the script which in order for ForceMode2D.Impulse. This is because Impulse uses the mass of the Rigidbody for a one time boost of force applied onto the game object. I have gotten the method to work on a single game object that was already in my scene, but I wonder if it cant get applied to a prefab that is not currently instantiated.

I’ve tried to get this too work by adding the method too the instantiation script and the homing missile script but it haven’t worked. Also in each script I’ve moved the method in multiple places to try and get it too work. I have also looked at previous forms, but I haven’t got any of the suggestions mentioned there to work.

TLDR: How do I instantiate a prefab with an initial burst of speed using ForceMode2D.Impulse?

I’m open to here alternative ways in solving this problem but, as for the future of this game I do want to keep the missiles physics based and that’s part of the reason I like ForceMode2D.Impulse.

Hmm, doesn’t look like you’re getting the rb for the object that you just made. Here’s code that I use

``````   GameObject newBall = Instantiate(cannonBallPrefab, transform.position, cannonBallPrefab.transform.rotation);
Rigidbody newBallRb = newBall.GetComponent();
float speed = Random.Range(minForce, maxForce);
``````

for shooting a cannon.

Hi everyone,

Update on the Issue, I thought I’d make a post since I know beginners also look at these posts like I do.

I found that the problem wasn’t that I couldn’t get the ForceMode2D.Impulse too work. I did

``````    GameObject newAttack = Instantiate(attackPrefab, firePoint.position, firePoint.rotation);