• 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 Stef · Jun 18, 2012 at 08:59 PM · raycastinstantiateoffset

raycast object instance offset?

Lads,

I'm using raycast and mousedown to swap one gameobject for another. All is working, but the instantiated object comes in offset (I'd like it to come in the exact position of the gameobject it's replacing). I think the problem has to do with the hit.point of the ray or the destruction of the gameObject... not sure. Anyway, here's the code:

 function Update () {
 
     var hit : RaycastHit;
         if(Physics.Raycast (transform.position,
                         transform.forward, hit, 500)) {
         if (hit.collider.gameObject.tag=="Block" 
         && Input.GetMouseButtonDown(0) == true) {
         var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 500;
         Debug.DrawRay (transform.position, forward, Color.green);
         
             //var instance2:GameObject = Instantiate (Resources.Load
             //("Detonator_explosion1"),transform.position, transform.rotation);
             var instance:GameObject = Instantiate (Resources.Load
             ("building_stage_2_test"),hit.point, Quaternion.identity);
             Destroy (hit.collider.gameObject);    
     
         }
    }
   }

Any thoughts?...

ps - the pivot locations of the object being instanced and the object being destroyed match.

Thx,

Stef

Comment
Add comment · Show 6
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 You! · Jun 18, 2012 at 09:02 PM 0
Share

Firstly, you should get rid of the "== true" since "Input.Get$$anonymous$$ouseButtonDown(0)" returns true or false anyway (so the check is unnecessary).

avatar image Wolfram · Jun 18, 2012 at 09:10 PM 0
Share

$$anonymous$$ore importantly, put the Input.Get$$anonymous$$ouseButtonDown(0) test as the outermost "if", otherwise you're wasting ressources every frame casting rays, ins$$anonymous$$d of just the single(!) frame where the mouse button is actually pressed.

avatar image Stef · Jun 18, 2012 at 09:14 PM 0
Share

@Wolfram... what do you mean by the 'outermost' if... thanks.

avatar image Wolfram · Jun 18, 2012 at 09:23 PM 0
Share

Just move the Input.Get$$anonymous$$ouseButtonDown(0) test out of your block, and make it the very first thing that's tested in Update(). This way, if the button is not pushed (which is probably 99% of your game time), this is the only test that will be performed, and everything within that "if" is never executed. Your current code does a Physics.Raycast() every frame, and then, several lines below, tests the mouse button, and says, "oh well, the button wasn't pressed anyway, so that's that".

avatar image Stef · Jun 18, 2012 at 10:38 PM 0
Share

@Wolfram... like this?:

function Update () {

     if Input.Get$$anonymous$$ouseButtonDown(0)
     var hit : RaycastHit;
     if(Physics.Raycast (transform.position,
                     transform.forward, hit, 500)) {
     if (hit.collider.gameObject.tag=="Block" 
      {
     var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 500;
     Debug.DrawRay (transform.position, forward, Color.green);
     
         //var instance2:GameObject = Instantiate (Resources.Load
         //("Detonator_explosion1"),transform.position, transform.rotation);
         var instance:GameObject = Instantiate (Resources.Load
         ("building_stage_2_test"),hit.transform.position, Quaternion.identity);
         Destroy (hit.collider.gameObject);
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Wolfram · Jun 18, 2012 at 09:11 PM

Well, you're instancing it at the exact hit.point position. Use hit.transform.position instead, which is the pivot of the object that was hit.

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 Stef · Jun 18, 2012 at 10:47 PM 0
Share

I changed the hit.point to:

var instance:GameObject = Instantiate (Resources.Load ("building_stage_2_test"),hit.transform.position, hit.transform.rotation);

but am still getting the same result?...

Thx

avatar image Wolfram · Jun 19, 2012 at 10:25 AM 0
Share

Hm, the only explanation I can think of is that the objects really have a different pivot. If you drag both the original object and your "building_stage_2_test" into the Hierarchy view (so that they have identical values in their transform), are the really placed on top of each other, the way you want them to?

Or you actually hit a different object, interfering with it. Is the original object composed of multiple GameObjects/colliders/sub-objects?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Offset of texture with Raycast 1 Answer

help with scripting combo points 1 Answer

Help with a different approach to instantiating weapons? 1 Answer

how to instantiate with an offset? 1 Answer

How do I instantiate an object in an RTS style game? 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