- Home /
Bullets spawn behind plane?
So I've completely reconsidered the way I'll make my scrolling shooter. I was originally planning on making it with simple 2D gameplay, but then I realized it'll be quicker if I make my Starfox clone straight away.
So I managed to get most stuff working, I just have one problem, which I can't get around. I've managed to make the plane fly, I managed to get it to shoot in the direction it's facing. All fine and well. Except that the bullets seem to spawn behind the canon prefab. You can see the problem in the video below: Video Link Anyone know what the problem is? Here's the code for the cannon that instantiates the bullet:
using UnityEngine;
using System.Collections;
public class PlayerCannon : MonoBehaviour {
public Rigidbody bullet;
public float velocity = 10.0f;
public Transform Player;
// Use this for initialization
//void Start () {
//}
// Update is called once per frame
void Update () {
if (Input.GetButton ("Fire1")) {
Rigidbody newBullet = Instantiate (bullet, Player.position, Player.rotation) as Rigidbody;
newBullet.AddForce(transform.forward*velocity, ForceMode.VelocityChange);
}
}
}
What are you assigning to the Player variable? The ship, or a spawnpoint transform ? If it's the ship, then you need to either change it to a spawnpoint transform that you position where you want it to spawn at, or offset the spawn position (Player.position) to spawn it where you want it.
Answer by Bunnybomb7670 · Mar 01, 2014 at 09:59 PM
An alternate way to position you're bullets properly is to store a gameObject, an empty one on you're ship and position it to where you want you're bullets to come from, then instead of instantiating it from the ships position, instantiate it from the position of the empty gameObject.
It might not be clear from the code, but that's precisely what I'm doing. I've set the Player variable to my two canon prefabs. That's why you can see the plane firing two bullets.
$$anonymous$$aybe your ship is outrunning the bullets. Slow the ship way down and fire a few rounds to see where they spawn at.
Yes, I think you're right. The bullets seem to spawn standing still, and then accelerate up to speed. Any way I can make them spawn at their target speed?