Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Rick74 · Aug 17, 2013 at 06:12 PM · javascriptinstantiateoffset

how to instantiate with an offset?

Hey guys, I'm trying to instantiate one object to the position of another but with an offset.

I've fudged around a bit;

 if ( Input.GetMouseButtonDown (0) && lastHitObj )
            {
                if ( lastHitObj.tag == "placementPlane_Open" )
                {
                    if ( structureIndex == 0 )
                    {
                        Instantiate ( allStructures[structureIndex], lastHitObj.structurePOS, Quaternion.identity );
                        structurePOS = Vector3 ( transform.position.x + .5, transform.position.y + .5, transform.position.z );
                    }

of course this is giving me errors :( I've done a search and previous questions answers must not be in javascript because they cough out errors.

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 Rick74 · Aug 17, 2013 at 06:15 PM 0
Share
 if ( structureIndex == 0 )
                    {    
                        Instantiate( allStructures[structureIndex], lastHitObj.Vector3(pos.x,pos.y+2,pos.z), Quaternion.identity );                    
                    }

Tried this as well, but that's also giving me errors.

1 Reply

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

Answer by JoaquinRD · Aug 17, 2013 at 06:19 PM

First of all, you mis-spelled "structurePOS" in the Instantiate call.

Also, try applying the offset like this:

 var offset : Vector3 = new Vector3(0.5f, 0.5f, 0);
 Instantiate(allStructures[structureIndex], lastHitObj.structurePOS + offset, Quaternion.identity );
Comment
Add comment · Show 6 · 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 Rick74 · Aug 17, 2013 at 06:21 PM 0
Share

thanks JoaquinRD, I noticed I did a type-o when I tried to retype the code out. (next time I'll just copy/paste) I'll give that a try!

Can I ask a quick question about your solution though? Being new to code, i don't understand why sometimes people put "f" beside the number. What does that do?

avatar image JoaquinRD · Aug 17, 2013 at 06:27 PM 1
Share

It converts the number to a float from a a double, two different types of numbers. Decimal numbers are doubles by default and the x, y, and z in Vector3 are floats, so the f is needed to make sure the number is the right type (float). I'm not sure if this is necessary in JavaScript since it might convert it automatically, but I know that it is necessary in C#. $$anonymous$$ake sense?

avatar image Rick74 · Aug 17, 2013 at 06:33 PM 0
Share

Yeah that makes sense, thanks for explaining that!

I did some adjustments to the answer you gave me as you created the var "offset" which replaces my old var "structurePOS".

code looks like this now;

 if ( structureIndex == 0 )
                    {    
                        var offset : Vector3 = new Vector3(0.5f, 0.5f, 0);
                        Instantiate(allStructures[structureIndex], lastHitObj.transform.position + offset, Quaternion.identity );                    
                    }

And works like a charm! Thanks for you help!

avatar image ArkaneX · Aug 17, 2013 at 06:35 PM 0
Share

This instructs compiler about the exact type of the constant. F (or f) stands for 'float'. If you skip this, then in C# number like 0.5 is treated as double, and number like 5 as int. I believe in JavaScript, 0.5 is treated as float by default.

avatar image Rick74 · Aug 17, 2013 at 06:36 PM 0
Share

Ok so then to clarify, it's not needed in JavasScript? ( UnityScript )

Show more comments

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

17 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

Related Questions

Instantiate prefab within parameters?(Javascript) 1 Answer

How to keep instatiated object above a certain height? 1 Answer

Array won't be filled by Instantiate 1 Answer

iTween NullReferenceException error 1 Answer

Help with my On_Bullet please 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