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 epepstein · Apr 30, 2012 at 08:18 PM · instantiatepositionspawn

Spawning a prefab X units in 1 direction from an object

Hi,

I'm trying to spawn a prefab explosion at the end of a cannon when i click.

I have:

void OnMouseUp () {

     Instantiate(BlueExplosionPrefab, transform.position, Quaternion.identity);
 }

This of course spawns the explosion at the origin of the Cannon. How do I make it do something like spawn the explosion at the origin+X units on a given axis?

Thank You.

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 Lo0NuhtiK · Apr 30, 2012 at 08:20 PM 0
Share

why not just put an empty game object at the spot you want it to spawn at, and instantiate it there?

avatar image epepstein · Apr 30, 2012 at 08:27 PM 0
Share

I honestly don't even know how to get that position. I've been using unity for about 3 days now and I've never programmed anything ha. I'll try to look that up.

avatar image epepstein · Apr 30, 2012 at 08:29 PM 1
Share

O$$anonymous$$! I got it thanks! I had previously tried something like that but in my noob fashion did transform.position.spawnpoint ins$$anonymous$$d of spawnpoint.transform.position

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by BiG · Apr 30, 2012 at 08:28 PM

Follow Lo0NuhtiK's advice, in his comment, or, if you want to mantain your method, do the following:

 pos = transform.position;
 Instantiate(BlueExplosionPrefab, Vector3(pos.x + 5, pos.y ,pos.z), Quaternion.identity);

This will instantiate the explosion in the same point as the origin, but with an offset of 5 units on the X axis.

Comment
Add comment · Show 2 · 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 Lo0NuhtiK · Apr 30, 2012 at 08:38 PM 1
Share

Upvoted for ninja * 2 :D

avatar image BiG · Apr 30, 2012 at 08:40 PM 1
Share

Ahah! Thanks Lo0Nuthi$$anonymous$$! ;)

PS: Glad to see you again here...^_^

avatar image
1

Answer by stingman · Apr 30, 2012 at 08:29 PM

You can always do what LoONuhtiK said but to answer your question in the way you want to do it... just add a Vector3 addition to your code:

 Instantiate(BlueExplosionPrefab, transform.position + Vector3(10,0,0), Quaternion.identity);

That's the simplest explanation. That will instantiate the prefab 10 units to the right on the X axis. Make sense? Just alter the code to fit your needs.

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
1

Answer by Seth-Bergman · Apr 30, 2012 at 08:30 PM

like loonuhtik says, you could just place an empty object exactly where you want the explosion, that would be the easiest way:

var explosion : Transform;

void OnMouseUp () {

 Instantiate(BlueExplosionPrefab, explosion.position, Quaternion.identity);

}

or, to answer the original question, you would say:

void OnMouseUp () {

var newPosition = transform.position; newPosition.x += (whatever number here); Instantiate(BlueExplosionPrefab, newPosition, Quaternion.identity); }

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

7 People are following this question.

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

Related Questions

Place object randomly at any predefined spawn point 1 Answer

Why does this script spawn 13 ClickEffect prefabs instead of 1? 2 Answers

Help making a damage number system 1 Answer

Position + Vector3 doesn't return correct values 1 Answer

Spawn: Instantiate GameObject 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