- Home /
Bullets Based on Orientation
So I'm trying to set up a system where I have "GunNodes" which spawn bullets and fire them in the direction that the nodes are facing. So far, I've been having some trouble.
BulletCode:
function Update () {
LifeTime += Time.deltaTime;
transform.position += Vector3.forward * BulletSpeed * Time.deltaTime;
if(LifeTime >= Range){
Destroy(gameObject);
}
GunCode:
function FireGuns (){
FiringCoolDown = true;
Instantiate(BulletObj, GunNode.position, GunNode.rotation);
Handheld.Vibrate ();
yield WaitForSeconds(FireRate);
FiringCoolDown = false;
}
Particularly, how can I spawn an object and cause it to move in the direction that another object is facing?
Answer by syclamoth · May 22, 2012 at 02:55 AM
In your bullet code, change this line:
transform.position += Vector3.forward * BulletSpeed * Time.deltaTime;
to this:
transform.Translate(Vector3.forward * BulletSpeed * Time.deltaTime, Space.Self);
Keep in mind that bullets that move in this way will not respond to colliders, or in fact any kind of collision detection. You will have to add a second step that sends a linecast between the bullet's position before and after the translation to manage collisions.
Wow that works fantastic!
Now, about the linecast. I'm developing this game for Android, any thoughts on how much impact a linecast will have on framerate if it's being called constantly?
Not significant. It's way more efficient than normal collision detection, anyway.
Your answer
Follow this Question
Related Questions
Rotation and Velocity of bullets. 1 Answer
Going crazy over simple bullets not instantiating in the correct position 0 Answers
Bullet not dissapearing after time ? 3 Answers
Problem with Shooting Accuracy 0 Answers
making bullet go travel at angle fired and limit gun rotation in Unity C# version 5.2 2D 1 Answer