• 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 RyanJ · Mar 10, 2014 at 10:37 PM · javascriptinstantiaterelative

Instantiate to relative location?

 #pragma strict
  
 var objectName : GameObject;
 //var player : GameObject;
 function Start()
 {
  
  
 }
  
 function Update()
 {
 if(Input.GetKeyDown(KeyCode.X)) {
 var pos = Vector2(15,1.5); 
      var rot : Quaternion = Quaternion.identity; 
      Instantiate(objectName,pos,rot);
 }
 }

I'm creating a 2D infinite runner. This script will create a block at (15,1.5), but what I'd like to do is have this block appear at this location relative to the player,since they will always be running forward. I've been at this for awhile and just can't figure it out, since I'm pretty new to all of this. Any help is greatly appreciated!

Comment
Add comment · Show 1
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 getyour411 · Mar 10, 2014 at 10:45 PM 0
Share

You were much closer in the other thread

http://answers.unity3d.com/questions/659945/instantiate-a-prefab-with-a-key-press-to-a-relativ.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dechcaudron · Mar 10, 2014 at 10:56 PM

I haven't ever created 2D games in unity, but in 3D what you'd do is to Instantiate(objectName, pos+player.transform.position, rot).

That should make it. Tell me if it worked buddy.

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 RyanJ · Mar 10, 2014 at 11:14 PM 0
Share

It works perfectly!

 #pragma strict
  
 var objectName : GameObject;
 var player : GameObject;
 function Start()
 {
  
  
 }
  
 function Update()
 {
 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
 var pos = Vector2(15,2); 
      var rot : Quaternion = Quaternion.identity; 
      Instantiate(objectName,pos+player.transform.position, rot);
 }
 }

Thank you so much both of you!

avatar image Dechcaudron · Mar 11, 2014 at 12:18 AM 0
Share

Awesome! $$anonymous$$eep on program$$anonymous$$g! Practice makes masters!

avatar image
0

Answer by Twin-Mobile · Mar 10, 2014 at 10:58 PM

you need to instantiate the object (15,1.5) from the player?

if yes then do this

 position = new Vector2(player.transform.position.x+15,player.transform.position.y+1.5);
 Instantiate( objectName,position,rot);
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 RyanJ · Mar 10, 2014 at 11:29 PM 0
Share

This works as well, thanks!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate a prefab with a key press to a relative location? 1 Answer

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

How to Select between two prefabs randomly ? 1 Answer

Simple Instantiate gameObject problem 3 Answers

Instantiate object in for loop with array 2 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