- Home /
Force is not applied to the rigidbody?
using UnityEngine;
using System.Collections;
public class PlayerGunController : MonoBehaviour
{
public GameObject bullet;
public int speed;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Update ()
{
if(Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(bullet, shotSpawn.position, shotSpawn.rotation);
bullet.rigidbody.AddForce(Vector3.forward * speed);
}
}
}
This is the script I am using to fire a gun. "bullet" is a sphere, with a rigidbody component. The Rigidbody has useGravity and isKinematic off. When I click the mouse button, a sphere is spawned, but just floats there, unmoving. If I spawn another sphere overlapping the first, they both begin to move, floating off into space. This happens regardless of the number I assign as "speed", whether it's 100 or 1000000000.
Comment
Best Answer
Answer by Lylek · Sep 17, 2014 at 03:15 AM
Create another GameObject variable and have it equal the Instantiation. Then add force to it, not the prefab.
http://answers.unity3d.com/questions/559014/object-after-instantiate-doesnt-apply-addforce.html