- Home /
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 (https://www.youtube.com/watch?v=0v_H3oOR0aU&t=302s) 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()
{
rb.AddForce(new Vector2(5, 0), ForceMode2D.Impulse);
}
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);
//rb.AddForce(new Vector2(5, 0), ForceMode2D.Impulse);
}
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. Thank you for reading!
Answer by Askal_Vermilion · Dec 17, 2020 at 12:29 AM
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);
newAttack.GetComponent<Rigidbody2D>().AddForce(transform.up * thrust, ForceMode2D.Impulse);
//thrust is a public float variable at the top of the script
This line of code should work perfectly but, my problem resulted between the script of my prefab and the instantiation script. Unity is technically is running the AddForce line of code, but when my Prefab loaded in its script overrides the velocity made by the instantiation script thus not prompting it too shoot forwards like I want it too. I'm still questioning how I can incorporate it with my prefab script, but as for now I hope this helps.
Answer by CmdrZin · Dec 15, 2020 at 05:31 AM
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);
newBallRb.AddForce(transform.up * speed, ForceMode.Impulse);
for shooting a cannon.
public Transform firePoint;
public float thrust;
public Rigidbody2D rb;
public GameObject attackPrefab;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Shoot()
{
var newAttack = Instantiate(attackPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = newAttack.GetComponent<Rigidbody2D>();
rb.AddForce(transform.up * thrust, Force$$anonymous$$ode2D.Impulse);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
Shoot();
}
}
Thank you for your response but, sadly I still cannot get it working even with your method. When I tried to incorporate it into my instantiation script unity gave me a few errors so I did my best to try and resolve them. I used a variable to store newAttack and i made a public float for the speed variable.
Its strange I notice that every time i go into play mode the rigidbody reference disappears off the script. I'll show you in pictures.
After I exit game mode it goes straight back to the first image. Do you why this is happening?
Nothing pops out except the redeclaration of rb. Take a look at
https://learn.unity.com/project/c-scripting-in-unity-the-observer-pattern
The example game is like yours.
Thanks for your help, I couldn't get your line of code to work, but it helped me in the right direction.
Your answer
Follow this Question
Related Questions
Rigidbody.AddForce not working 1 Answer
Using rigidbody.AddForce and speeding the object 2 Answers
Velocity and AddForce Problems 0 Answers