- Home /
Bullet not always shooting in forward direction. Code included.
public Rigidbody bullet;
public float speed;
Rigidbody clone;
void Update () {
if (Input.GetKeyDown("space")){
clone = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.forward * speed * Time.deltaTime;
Destroy(clone.gameObject, 15);
}
}
My bullet is not always in the forward direction. Sometimes it doesn't even shoot. Please help and thanks
we might need some more information, but the issue could be that the object you are instantiating from probably has a rigid body, and by instantiating the bullet object inside of it, you get some funky behaviour on your bullets velocity
Answer by whydoidoit · Apr 08, 2013 at 08:21 AM
@Prodigga has a good point in the comments plus if for instance you instantiate the bullet rigidbody directly ontop of whatever is shooting it and that thing has a collider then all hell will break loose :)
You might want to try
clone = Instantiate(bullet, transform.position + transform.forward * 1, transform.rotation) as Rigidbody
Where you should change the * 1 to be the size of the thing that is doing the shooting/2 + a fudge factor.
Answer by RyanZimmerman87 · Apr 08, 2013 at 08:20 AM
My first guess is that you should use Quaternion.identity instead of transform.rotation
so the line of code:
clone = Instantiate(bullet, transform.position, transform.rotation);
might need to be:
clone = Instantiate(bullet, transform.position, Quaternion.identity);
Another possibility is that you need to ignore collision for the bullet with the object it is shot from like this but with correct variables for your project:
Physics.IgnoreCollision(bullet.collider, player.collider);
Your answer
Follow this Question
Related Questions
Can't remove instantiated prefab 0 Answers
Variables stay overwritten in inspector 1 Answer
NullReferenceException Transform.Find 1 Answer
Destroying & Then Spawning Quickly 1 Answer