- Home /
Question by
kenjammin · Feb 02, 2014 at 03:58 PM ·
instantiatevector2
Bullets will instantiate but they won't move.
bullets intantiate with no problem but when i try to apply forces they don't move. I have the prefab connected and rigidbody2d on the bullet prefab. Thank you for your time/help.
if(facingright == true){
var bulletInstance : GameObject = Instantiate(bullets, transform.position, transform.rotation);
var bulletInstanceRigidbody: Rigidbody2D = bulletInstance.GetComponent(Rigidbody2D);
bulletInstanceRigidbody.velocity = new Vector2(10, 0);
}
Comment
Answer by sandhillceltic · Feb 02, 2014 at 09:53 PM
I'm taking an educated guess here, but I think that you are retrieving the velocity of the bullet, rather than pushing it. Try using
rigidbody.AddRelativeVelocity(Vector3). Check the spelling on that
This should add velocity and make your bullet move.
EX
if(facingright == true){
var bulletInstance : GameObject = Instantiate(bullets, transform.position, transform.rotation);
var bulletInstanceRigidbody: Rigidbody2D = bulletInstance.GetComponent(Rigidbody2D);
bulletInstanceRigidbody.AddRelativeVelocity(10, 0, 0);
}
This works in 3d, I have never tried it in 2D.
Good luck
Your answer