1
votes

Currently instantiating an object (a prefab) at run time via a button click. The object instantiates each time I click the button but it doesn't move even though I have added force to it.

Checked similar issues where its a mismatch of casting between GameObject and Rigidbody2D but this isn't the issue in my case. Tried switching between transform right and forward multiplied by a large number in case the value was too small for a change which doesn't make a difference either. Any help is appreciated. Thank you.

Following script is on the Player object. The prefab is instantiated from the object 'tip' which is a child object on the Player object. Rigidbody2D is set to Kinematic for bullet.

 public Transform tip;
 public Rigidbody2D bullet;

 if (Input.GetButtonDown("Fire1")) {
    Rigidbody2D clone;
    clone = Instantiate(bullet, tip.position, Quaternion.identity) as Rigidbody2D;
    clone.AddForce(clone.transform.right * 5000000);
 }
1

1 Answers

1
votes

Rigidbody2D is set to Kinematic for bullet.

This is the mistake: the physics engine never moves Kinematic objects, change the bullet rigidbody body type to Dynamic, and the force you apply with AddForce will move the bullet.