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 Tyler 2 · Nov 05, 2010 at 06:00 AM · objectspawnbullet

Spawn an object?

Hello, this is a simple question, but how would I spawn an object at a certain velocity and position (relative to another object). Like a bullet firing from ship for a 2D shooter? Thanks.

Comment
Add comment · Show 3
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 lampshade · Nov 05, 2010 at 06:24 AM 0
Share

Your bullet (or whatever you want to 'instantiate') should be a 'Child' or 'Sub-Object' of your GameObject 'Ship' (or w/e na$$anonymous$$g convention you chose).

avatar image lampshade · Nov 05, 2010 at 06:25 AM 0
Share

This would make it very easy to get the bullet firing "FRO$$anonymous$$" the ship. A little fiddling around and you should have it in the position that you desire.

avatar image BerggreenDK · Jun 16, 2011 at 11:48 PM 0
Share

what if he moves the ship? wouldnt the bullet move with the ship? thats bad I think.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by zannghast · Nov 05, 2010 at 06:53 AM

Further to lampshade's answer, here is an example of how you can use instantiate and the sort of firing mechanism you may want to look into:

// make sure that the ammo is where it's supposed to be GameObject ammoClone = (GameObject)Instantiate(ammo, startPointTransform.position);

// "firing" effect. The last parameter, ForceMode.VelocityChnage, is crucial in mimicking the initial velocity of a 'fired' object such as a cannonball or bullet. ammoClone.rigidbody.AddRelativeForce(startPointTransform.forward * BlastForce, ForceMode.VelocityChange);

The above code, is a snippet. It assumes the following conditions:

  • ammo is a reference to the object acting as the ammo.
  • The ammo object has a rigidbody component attached to it.
  • startPointTransform is a reference to the Transform of the object acting as the origin or start point of the ammo object
  • BlastForce is a float which you could control to adjust the initial force applied to the ammo
Comment
Add comment · Show 1 · 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 Joshua · Jun 17, 2011 at 12:52 AM 0
Share

Note that this is in C#, not JS. It also doesn't add a velocity relative to the other object. To do that you'd have to first set the velocity to equal the other object and then add the force.

avatar image
0

Answer by lampshade · Nov 05, 2010 at 06:23 AM

To Bring something into existance? Instantiate! http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

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

2 People are following this question.

avatar image avatar image

Related Questions

dragging out an object from mouse click? 1 Answer

Help! Can't destroy my bullet object because is a rigidbody 1 Answer

cube damage 2 Answers

Updating prefab information live 2 Answers

Spawn object with different material. 0 Answers


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