[SOLVED] Cannot AddForce to instantiated game object.
Hi,
I am attempting to add force to the rigidbody of an instantiated game object.
Here is my code:
void Update () {
Shoot();
}
void Shoot()
{
float x = spawnPoint.transform.position.x;
float y = spawnPoint.transform.position.y;
float z = spawnPoint.transform.position.z;
spawnVector = new Vector3(x, y, z);
if (Input.GetMouseButtonDown(0))
{
bulletInstance = Instantiate(bullet, spawnVector, Quaternion.identity) as GameObject;
bulletInstance.AddComponent<Rigidbody>();
bulletRB = bulletInstance.GetComponent<Rigidbody>();
bulletRB.AddForce(Vector3.forward * bullVel * Time.deltaTime);
}
}
Currently what occurs is a bullet is instantiated at the spawn point and simply falls to the ground. There appears to be no force being applied the bullet.
Does anyone have any input as to why the force is not being applied?
(Side note: the variable bullVel is equal to 50f)
Thank you for any input.
Answer by Jhyatt · Feb 04, 2016 at 06:06 AM
I found the solution to my issue!
Multiplying by Time.deltaTime was causing the force applied to the bullet to be so small that there appeared to be no force applied to it at all. I set my bullVel variable to a whopping 50,000f and the bullet moved as I expected.
I also found that using Vector3.forward caused my bullet to travel the same direction regardless of where my gun was facing. To fix that I used transform.forward instead.
Here is my working code:
void Update () {
Shoot();
}
void Shoot()
{
float xDir = spawnPoint.transform.position.x;
float yDir = spawnPoint.transform.position.y;
float zDir = spawnPoint.transform.position.z;
spawnVector = new Vector3(xDir, yDir, zDir);
if (Input.GetMouseButtonDown(0))
{
bulletInstance = Instantiate(bullet, spawnVector, Quaternion.identity) as GameObject;
bulletInstance.AddComponent<Rigidbody>();
bulletRB = bulletInstance.GetComponent<Rigidbody>();
bulletRB.AddForce(transform.forward * bullVel);
}
}
EDIT --- Here is better code for bullet collisions!
If you are trying to do the same thing I am and you intend on your bullets colliding with objects I would encourage you to use a raycast instead.
What was happening that caused me to go with the raycast was my bullets were moving so fast that Unity would sometimes not detect the collisions and the bullets would fly straight through objects.
To prevent this I am using a raycast and detecting collisions with the ray and then instantiating my bullet directly on the point where the ray collided with the object. This ensures your bullet will collide with the object.
Here is my code for that:
private RaycastHit hit;
void Update () {
Shoot();
}
void Shoot()
{
float xDir = spawnPoint.transform.position.x;
float yDir = spawnPoint.transform.position.y;
float zDir = spawnPoint.transform.position.z;
spawnVector = new Vector3(xDir, yDir, zDir);
forwardDir = transform.forward;
if (Input.GetMouseButtonDown(0))
{
Ray aimRay = new Ray(transform.position, forwardDir);
if (Physics.Raycast(aimRay, out hit))
{
bulletInstance = Instantiate(bullet, hit.point, Quaternion.identity) as GameObject;
bulletInstance.AddComponent<Rigidbody>();
}
}
}
Your answer
Follow this Question
Related Questions
Accessing the rigidbody of a cloned Object? 1 Answer
Later grab doesn't jump 0 Answers
Kinematic sphere doesn't move 1 Answer
Why does my character sometimes jump higher than other times? 1 Answer