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
0
Question by blackmethod · Mar 04, 2013 at 11:27 PM · rotationspawndirectionbullet

Spawn bullet in front of object then apply force?

Hey there I can't seem to get my bullet to spawn in front of my player, then apply the force. I'm doing a top down space shooter but the object creates inside my ship and also doesn't go straight. It goes off to the left instead of forward. Here's my current code. Hope you guys can help.

     var Bullet : Transform;
    
      
     function Update ()
     {
     if(Input.GetButtonDown("Fire"))
     {
     Instantiate(Bullet,transform.position, Quaternion.identity);
      
     Bullet.rigidbody.AddForce(transform.forward * 15);
     }
     }
Comment
Add comment · Show 7
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 hoy_smallfry · Mar 04, 2013 at 11:43 PM 0
Share

is transform the ship's transform?

avatar image blackmethod · Mar 04, 2013 at 11:48 PM 0
Share

The script is attached to my ship and runs off of it so I would assumed yet.

avatar image ByteSheep · Mar 05, 2013 at 12:14 AM 0
Share

Try:

 Bullet.rigidbody.AddRelativeForce(transform.forward * 15);

Should use the ships forward direction ins$$anonymous$$d of world space.

avatar image hoy_smallfry · Mar 05, 2013 at 12:20 AM 0
Share

Also, you should use an offset from the position so its not at the center of the ship:

 var offset : Vector3 = // use some offset values here

 Instantiate(Bullet, transform.position + offset, ...
avatar image highpockets · Mar 05, 2013 at 12:23 AM 0
Share
 Instantiate(Bullet,new Vector2(transform.position.x,transform.position.y + distanceFromCenterOfShipToGun, Quaternion.identity);

add the distance from centre of ship to gun to the y position and it should offset to the right place and the Quaternion.identity should be set to the correct angle. Try transform.rotation in place of Quaternion.identity

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Daphoeno · Mar 05, 2013 at 07:02 PM

 var Projectile : Rigidbody;
 var Spawn : Transform;
 var Force : int = 1000;
 
 function Start ()
 {
     gameObject.SetActiveRecursively(true);
 }
 
 function Update ()
 {
     if(Input.GetKeyDown(KeyCode.Mouse0))
     {
         Toss();
     }
 }
 
 function Toss()
 {
     gren = Instantiate(Projectile,Spawn.position,Spawn.rotation);
     gren.rigidbody.AddForce(transform.forward * Force);
     yield WaitForSeconds(0.2);
 }
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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make my new Instances spawn in the right direction? 1 Answer

Shoot an object and have it move based on rotation 1 Answer

Can't make a bullet move in the direction the actor is facing 2 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


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