- Home /
3d spaceship scene need help making projectiles fire forward
Hey guys. Just need a bit of help really.
Im in the middle of making an asteroids like game with a 3rd person view i have attached the camera to the ship and have it all moving about and what not, but i have a huge problem when i come to firing my projectiles.
I need my projectiles to fire forward in to space and hit the asteroids and blow them up. I do have a script that makes my gun fire forward but when i rotate the bullets start bending off like the guns rotation isnt keeping up with the ships rotation and also when i start moving they look like they stop firing alltogether even though they are still being instantiated.
Im not the best at scripting so please keep it easy to read if you could thanks very much for looking!!
PS
if you need to see my scripts i can show them but god knows what you would make of them lmao
Do show your scripts please, without them Its hard to see where the problem may be
var projectile : Rigidbody; var initialSpeed = 20.0;
function Fire () {
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
}
function Update () {
if (Input.GetButtonDown("Fire1")) {
Fire();
}
}
i dont know how put it them boxes like others do but there hope you can help
Answer by Hexergirl · Jan 15, 2011 at 08:00 AM
Don't forget that you have an own speed when your ship is flying. So you have to get the speed of your ship and add it to the speed of your projectile. Say you are flying with 150 m/s and your projectiles are flying with speed of 20, they would just look as they are flying backwards because your ships speed is faster than your projectile. Think about it as your are driving with a car, window down and throw something in driving direction. I would get the head back into the car fast..... :)
Just hand in the speed of your ship to the script from your gun. Every time you change your speed you can just send a message at a function in your gun script with the actual speed. Like this: BroadcastMessage("Fire", youractualspeedofship) And it would fire all guns that have this function in it ;) Example from my ship script:
if(Input.GetKey("q"))
{
if(trustspeed < maxspeed)
{
trustspeed += 1;
BroadcastMessage ("SpeedChanged", trustspeed);
}
}
Now my launcher:
function SpeedChanged (goSpeed : float)
{
goTrust= goSpeed;
}
goTrust is the actual speed of the ship and used in the projectile speed in the Fire function:
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed+goTrust));
They will always fly the defined projectile speed value faster than the ship. And if you fire and turn after that, the rockets are flying in the original direction as they should.
Maybe that helps a bit.
greets Hexergirl
Answer by Justin Warner · Jan 06, 2011 at 06:23 PM
Are your projectiles (Bullets) a particle effect, or is it an actual game object?
I'd just make the bullet a prefab, instantiate the prefab with the same rotation as the ship, then put a force on it to go forward, rather than scripting it to go forward... If that makes sense.
Answer by Bob5602 · Jan 06, 2011 at 06:56 PM
var projectile : Rigidbody;
var initialSpeed = 20.0;
function Fire () {
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); }
function Update () { if (Input.GetButtonDown("Fire1")) { Fire(); } }
That is the code you posted (incase others want to edit it).
I would suggest you change your projectile from a rigidbody to a GameObject and like Justin said, use a prefab with a rigidbody component.
Additionally, you're setting the velocity manually of a rigidbody, and taking the transformdirection from your shooter transform. It seems like when you instantiate, the transform of the rigidbody (or GameObject in my case) is the same as the shooter, so the "forward" should be same for both. Then, instead of using .velocity being changed, I'd use
instantiatedProjectile.rigidbody.AddRelativeForce(transform.forward * initialSpeed);
I hope that helps, or at least brings everyone closer to help with your problem :)
hi thanks for ur replies erm my bullets are a prefab but im confused as to what i would actually end up changing lol.
heres another script i tried
var bulletPrefab : Transform; var shootForce =1000.0f;
function FireRocket () {
var instanceBullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
function Update () {
if (Input.GetButtonDown("Fire1")) {
FireRocket();
}
}
Honestly that script should work. I would change this line to : instanceBullet.rigidbody.AddForce(instanceBullet.transform.forward * shootForce) so that it adds the force on the forward of the bullet not the shooting transform, but otherwise it will work. Whats the issue you're having?
the problem im having is when i move my ship left and write the bullets to keep going forward they start shooting off to the side :/ i have no idea why.
How are you moving your ship? What object is the firing script on? Do they instantiate at the ship always, or do they appear off to the side?
im moving the ship with the arrow keys up down left and right and spacebar to go forward. i have the bullets instantiating from an empty object that is parented to the ship, and when i start my level they shoot forwards its only when i start turning that they also turn
thanks
Answer by Barabajagal · Oct 16, 2011 at 02:31 AM
Your bullets are moving forward, you're just in a rotating reference frame, which is creating centrifugal acceleration in your bullets. If you make them travel faster it will lessen the bending, but as long as you are in the process of rotating you'll see it.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Shooting Script Bullet Doesnt Fire 2 Answers
Projectile Script 1 Answer
Slow automatic shooting script? 2 Answers