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 Hikaru-Diamond · Jun 01, 2015 at 01:05 PM · prefabunity 2dinstantiate prefabsword

How can I make a prefab be Intantiated in a specific point of my player? I'm using Unity in 2D

So, I'm trying to Instantiate a prefab object, in this case a Sword, in front of my player (which in this prototype is a sprite of a green square)

alt text

To make the sword appear I used this code (Javascript):


 #pragma strict
 
 public var Sword : Rigidbody2D; //It specifies that the selected prefab is a Rigidbody2D.
 public var playerEnd : Transform; //This works as the reference for the location in which the weapon will appear.
 
 function Start () {
 
 }
 
 function FixedUpdate () {
     if(Input.GetKeyDown(KeyCode.J)){ //GetKeyDown for once the button/key is pressed, to activate the event again the player must press the button/key again.
         var swordInstance : Rigidbody2D; //This will summon the object as a rigidbody2D
         swordInstance = Instantiate(Sword /*This is the prefab*/ , playerEnd.position /*This is rhe position in which the prefab will be created*/, playerEnd.rotation /*This will stablish the orientation of the object, depends on the orientation of the player*/); 
     }
 }

For the playerEnd I tried to put my Game Object of my player, the Green Square, and it works (Kind of...), but the sword appear in the middle of the player.

![alt text

Just like this...

Soooo, how can I make the thing to appear in front of the player? Is there a way to do it?

player.png (152 B)
cube-sword-error.jpg (1.9 kB)
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 SterlingSoftworks · Jun 01, 2015 at 03:07 PM 0
Share

Something to try would be to create the sword as a child object of the player. Then you'll be able to move the sword's position by changing the position of the child object. And since it's a child of the player it will stay with the player at all times. :)

avatar image Hikaru-Diamond · Jun 01, 2015 at 07:55 PM 0
Share

Never thought about that before O.O $$anonymous$$ight as well try it ;) Thanks Sterling!

avatar image SterlingSoftworks · Jun 01, 2015 at 10:10 PM 0
Share

Np, crazy what you can take from some tutorials! :)

0 Replies

· Add your reply
  • Sort: 

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

What is the correct way to use prefab in my example? 2 Answers

I need to summon projectiles 2 Answers

How to instantiate prefab when colliding with specified prefab 1 Answer

Is it better to use ParticleSystems on one GameObject OR instantiate ParticleSystems? 1 Answer

Eliminating prefabs from resources folder? 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