- Home /
Why does this rigidbody not fire forwards?
I am trying to fire a rigid body forward, here is my code:
Instantiating the 'bullet':
ExplosiveBulletb = ObjectPoolManager.CreatePooled(bulletPrefab.gameObject, _bulletSpawn.position, _bulletSpawn.rotation * accuracySeed).GetComponent<BulletBase>() as ExplosiveBullet;
And here is the script I have attached to my bullet prefab (simple sphere object with a box collider, nothing special)
using UnityEngine;
using System.Collections;
public class ExplosiveBullet: MonoBehaviour {
public float speed = 10.0F;
public float lifeTime = 2.0F;
protected Transform _tr;
protected float _spawnTime = 0.0F;
public virtual void Start() {
_tr = transform;
_spawnTime = Time.time;
rigidbody.AddForce(_tr.forward * speed, ForceMode.Impulse);
}
void Update() {
if (Time.time > _spawnTime + lifeTime){
DestroyBullet();
return;
}
}
void Explode(){
Debug.Log("Booom!");
}
void OnCollisionEnter(Collision c){
Explode();
DestroyBullet();
}
public void DestroyBullet(){
ObjectPoolManager.DestroyPooled( gameObject );
gameObject.SetActiveRecursively(false);
}
}
For some reason, my bullet never seems to fire forward. It shoots off in strange directions which don't seem very relevant to my players facing. Any ideas?
Answer by henry96 · Aug 13, 2011 at 04:34 PM
the problem of shooting to a wrong direction is usually caused by wrong direction of the z axis (transform.forward). So check ur axis of spawn point for bullet (if u hav) or check other gameobjects which are related to the instantiate of the bullet!!! Hope this help
Your answer
Follow this Question
Related Questions
Bullet projectiles with collision info without affecting others 0 Answers
Issue with Instantiate and the instant that spawns on a moving object that rotates 1 Answer
Mesh Collider colliding with Primitives is jittery 1 Answer
Script not working after changing isKinematic 1 Answer
Shake Effect 0 Answers