- Home /
Bullet does not move forward after being initiated
I have two separate scripts. One that allows the enemy to fire bullet and one that detects bullet's collision with the player. The first one is applied on the enemy while 2nd on the Bullet prefab. When i use only the first script, the bullet behaves like a rigid body, moves forwards, collides with objects and gets deflected etc. However, after applying the 2nd one, the bullet just doesn't move forward at all and stays at its position until it gets destroyed. Please help me resolve this issue. Thanks in advance.
Script 1: (has more functions too, but i have shown just one)
void attack()
{
if (Time.time > nextFire) {
//shoot
GameObject tempBullet=Instantiate(bullet,gun.position,gun.rotation) as GameObject;
Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody> ();
tempRigidBodyBullet.AddForce (tempRigidBodyBullet.transform.forward * bulletspeed);
Destroy (tempBullet, 0.30f);
//play audio
bulletAudio_player.Play();
nextFire = Time.time + fireRate;
}
}
Script 2:(it has only one function)
void OnCollisionEnter(Collision col)
{
Destroy (gameObject);
if (col.gameObject.CompareTag ("Player"))
{
PlayerHealth.health -= 2;
}
}
Answer by Kciwsolb · May 18, 2018 at 07:46 PM
Hi, first of all, make sure you format all of your code as code. This was hard to read. Now, when you declare and initialize tempRigidBodyBullet you are doing this:
Rigidbody tempRigidBodyBullet = tempBullet.GetComponent ();
You are not specifying a Component Type. Use angle brackets after GetComponent and before your parameter list to specifiy a Type.
Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody>();
Hi. First, thanks for replying. Sorry for the issue due to improper formatting, but I am new to unity and did not know about code sample option but i have edited it now. Sir what you said was already in my code ( this i mean) but due to some reason was not shown in the question. Even after that its not working. What shall i do next? Thanks
Where are you initializing bulletSpeed? If it is somehow 0 in your code or the inspector, that could cause it.
No it is not 0. It is moving perfectly before applying the second script
Your answer
Follow this Question
Related Questions
Help with getting a bullet to move. 2 Answers
Bullet does not move forward 1 Answer
Control amount of bullets 2 Answers
Shooting & Animation Problem 1 Answer
I Want To Shoot Bullets Correctly. 2 Answers