Cant Get a Bullet to Shoot (C#)
So I'm making a third person shooter and right now I'm working on a simple enemy ai. So far the enemies look at you and shoot, but the bullets don't go anywhere. They spawn and drop to the ground. I'm using a piece of code I used when making the player shooting script, and I have all the right variables in the enemy script, but it's not working like it is in the player shooting script. The bullets just drop like I said. Here's the script: using UnityEngine; using System.Collections;
public class EnemyScript : MonoBehaviour {
public bool agroed;
public Transform player;
public Rigidbody bullet;
public float force;
public Transform bulletSpawn;
public float rateOfFire;
public float waitShootTime;
// Use this for initialization
void Start () {
agroed = false;
}
// Update is called once per frame
void Update () {
waitShootTime -= Time.deltaTime;
if(waitShootTime <= 0)
waitShootTime = 0;
if(Input.GetButtonDown("Play"))
{
agroed = true;
}
if(agroed == true)
{
transform.LookAt(player);
if(waitShootTime <= 0)
{
waitShootTime = rateOfFire;
Shoot();
}
}
}
void Shoot()
{
Rigidbody clone;
clone = Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation) as Rigidbody;
clone.velocity = bulletSpawn.transform.TransformDirection(Vector3.forward * force);
}
}
can any of you find out why the bullet is ignoring velocity? All variables are assigned. Thanks!
$$anonymous$$ight sound like dump question, have you check so unity doesn't set force to 0?
Answer by DiegoSLTS · Aug 30, 2015 at 05:19 PM
In this line:
clone.velocity = bulletSpawn.transform.TransformDirection(Vector3.forward * force);
Are you sure you want "Vector3.forward" there? Vector3.forward is a global direction, it's (0,0,1) in world space. The bullet will start with a velocity up in the Z axis and gravity will start pulling it down. Maybe you want to use transform.forward, which is the local forward direction of the enemy, the direction it's facing.
EDIT: Looking closer, maybe you want this:
clone.velocity = transform.forward * force;
or:
clone.velocity = bulletSpawn.transform.forward * force;
Answer by Seneral · Aug 21, 2015 at 07:57 PM
Use clone.AddForce instead off setting the velocity:)
Thanks for the suggestion. Sadly it didn't work. I appreciate you taking the time to help though!
@LABoy Just revisiting, actual problem is you are instantiating a Rigidbody which is a component, not a gameObject. Add it to a new GameObject prefab with a mesh and renderer + rigidbody.
@Seneral, what he's doing is O$$anonymous$$, Instantiate will clone the game object that has the Rigidbody component. That's why there's a version of Instatiate that receives an Object and needs a cast later, and a generics version that let's you specify the object type in code. You can even see in the docs examples using a Transform and a Rigidbody.
http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Your answer
Follow this Question
Related Questions
Set the Velocity to reach a position at a specific Timestamp 0 Answers
Check/Change velocity of an object 0 Answers
Rigidbody.velocity Movement code produce wildly different gravity interactions 0 Answers
Slow down a character while they are midair while keeping their original velocity 0 Answers
Please help me figure this out 0 Answers