Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by user-8029 (google) · Jan 06, 2011 at 05:45 PM · projectile

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bob5602 · Jan 06, 2011 at 06:03 PM 0
Share

Do show your scripts please, without them Its hard to see where the problem may be

avatar image user-8029 (google) · Jan 06, 2011 at 06:23 PM 0
Share

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

5 Replies

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image user-8029 (google) · Jan 06, 2011 at 07:06 PM 0
Share

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();
 }

}

avatar image Bob5602 · Jan 06, 2011 at 08:23 PM 0
Share

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?

avatar image user-8029 (google) · Jan 06, 2011 at 08:34 PM 0
Share

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.

avatar image Bob5602 · Jan 06, 2011 at 08:36 PM 0
Share

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?

avatar image user-8029 (google) · Jan 07, 2011 at 10:00 AM 0
Share

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

Show more comments
avatar image
0

Answer by Dzoni12 · May 31, 2011 at 06:09 PM

Nwqdqasdfashdqasnvgunb3t

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Projectile Firing From Floor, I Need Some Help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges