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 renaxi · Sep 14, 2014 at 01:35 PM · javascriptgameobjectraycast

Need help with building system

Hello guys and girls. I am currently making a survival game, just for the challange and i just started working on the building system, but i just ran into a problem i just can't seem to find information on this subject by google searching(might be searching the wrong words/terms).

Okay so is the code i have to far:

 var RayHit : RaycastHit;
 var RayPL : int = 10; //RayPlaceLenght
 var InventoryInfo : InventoryText;
 var Foundation : GameObject;
 
 
 function Start () 
 {
     InventoryInfo = gameObject.Find("Player").GetComponent(InventoryText);
 }
 
 function Update () 
 {
     var RayPlacing1 : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0)); //This is the center of the screen, the point where i want to place the object foundation.
     if(InventoryInfo.PlacingFDT == true)
     {
         if((Physics.Raycast(RayPlacing1, RayHit, RayPL)))  //Doing the raycast from the middle of the screen.
         {
             print("RaycastHit " + RayHit.collider.gameObject.name); //Just checking the name of the object i hit.
             if(RayHit.collider.gameObject.tag == "Terrain")  //if i hit the terrain that have the tag "Terrain", do the following.
             {
                 print("Insert prebuild foundation code here"); //This is the line i need help to do, i want to instantiate a single gameobject that is placed at the RayHit point, so i dont make multiple objects when i look around.
                 
                 if(Input.GetKeyDown(KeyCode.Mouse0)) //Press the mouse to build acutal object
                 {
                     Instantiate(Foundation, RayHit.point ,Quaternion.identity); //This is where i place the actual object.
                 }
             }
         }
     }
 }

So the thing i need help with is, how i place a single gameobject and then have it placed at the point where my RaycastHit hits the terrain. I tried with instantiate and it didn't work.

Thanks in advance.

Renaxi

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 _MGB_ · Sep 14, 2014 at 01:34 PM 0
Share

"... it didn't work."

We need more info. What are you seeing? Is the object created? Is it not in the right place? How does it differ from what you expected?

avatar image orb · Sep 14, 2014 at 02:13 PM 0
Share

Look in the editor window while it's running, select the instantiated object in the hierarchy and press F to find it.

avatar image renaxi · Sep 14, 2014 at 02:50 PM 1
Share

Sorry i should have said something else than "it didn't work". What i meant by it didn't work, was that the object (Foundation) is created on every frame if the raycast hits the terrain, and this doesn't work for me.

What i want the script to do, is to create a single object, that changes position according to where the RaycastHit, hits the terrain, not to create multiple objects everytime the RaycastHit hits the terrain.

1 Reply

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

Answer by Bluntweapon · Sep 14, 2014 at 05:27 PM

I'm assuming you mean you want to move the object after you've created it once.

So instead of

 Instantiate(Foundation, RayHit.point ,Quaternion.identity); //This is where i place the actual object.

You need to hold a reference to the object you have created.

 var objectIveMade : GameObject;
 
 function Update(){
 
    ...
    if(Input.GetKeyDown(KeyCode.Mouse0)) //Press the mouse to build acutal object
    {
        if( objectIveMade == null ){
            objectIveMade = Instantiate(Foundation, RayHit.point ,Quaternion.identity); //This is where i place the actual object.
        } else {
            objectIveMade.transform.position = RayHit.point;
        }  
     }
 }
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 renaxi · Sep 15, 2014 at 05:19 PM 1
Share

Thank you Blunt, was just what i was looking for! :-)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

[SOLVED] Only instantiating once 1 Answer

How to get info of object within certain range?(Javascript) 1 Answer

How to damage gameobject via raycast 1 Answer

Detect Object that are hit by a ray 3 Answers

Creating a teleportation gun 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