• 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 Digital-Phantom · Feb 11, 2015 at 02:54 PM · gameobjectraycastinstantiateterrainheight

Instantiated Objects not being set at ground/terrain level?(Solved)

Objects that I am instantiating into my game are being placed above the level of my terrain (not always the same height but about 6 - 7 units higher)

This is my script-

 using UnityEngine;
 using System.Collections;
 
 public class ObjectToFollowMouse : MonoBehaviour
 {
     
     public GameObject Target;
     public GameObject ActualPrefab;
     
     RaycastHit hit;
     
     private float raycastLength = 1000;
     
     void Start()
     {
         
     }
     
     
     void Update()
     {    
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         //Makes the 'Fake' Prefab follow the mouse.
         if(Physics.Raycast(ray, out hit, raycastLength))
         {
             Debug.Log(hit.collider.name);
             
             if(hit.collider.name == "Terrain")
             {
                 Target.transform.position = hit.point;
             }
         }
         
         //Instantiates Actual Building Prefab
         if(Input.GetMouseButtonDown(1))
         {
             GameObject TargetObj = Instantiate(ActualPrefab, hit.point, Quaternion.identity) as GameObject;
             TargetObj.name = "Target Instantiated";
             
             //Destroys the Fake !
             Destroy(gameObject);
         }
     }
 }
 

How can I ensure the units/objects are always going instantiate at ground level.

???

Comment
Add comment · Show 8
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 Digital-Phantom · Feb 11, 2015 at 03:13 PM 0
Share

ok.. get a few errors with that one -

Assets/Scripts/ObjectToFollow$$anonymous$$ouse.cs(31,88): error CS1061: Type UnityEngine.Transform' does not contain a definition for y' and no extension method y' of type UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)

Assets/Scripts/ObjectToFollow$$anonymous$$ouse.cs(31,48): error CS1502: The best overloaded method match for UnityEngine.Vector3.Set(float, float, float)' has some invalid arguments Assets/Scripts/ObjectToFollow$$anonymous$$ouse.cs(31,48): error CS1503: Argument #2' cannot convert object' expression to type float'

???

avatar image chariot · Feb 11, 2015 at 03:14 PM 1
Share

oh, sry, edited first comment. there must be

  specificVector.Set(hit.point.x, hit.collider.transform.position.y, hit.point.z);


And I think u must get upper then ground (cause char is centered by Y-coordinate), so get him upper ("1" for ex, u need to get this variable by yourself).

 specificVector.Set(hit.point.x, hit.collider.transform.position.y+1, hit.point.z);

avatar image Digital-Phantom · Feb 11, 2015 at 03:34 PM 0
Share

Oh..so close

Objects instantiating perfectly at terrain level now. BUT... they are not following the mouse after the initial selection.

This part of the script as I had it made them follow the mouse until the player right clicked the mouse

 //$$anonymous$$akes the 'Fake' Prefab follow the mouse.
         if(Physics.Raycast(ray, out hit, raycastLength))
         {
             Debug.Log(hit.collider.name);
             
             if(hit.collider.name == "Terrain")
             {
                 Target.transform.position = hit.point;
             }
         }
 
 
avatar image chariot · Feb 11, 2015 at 03:38 PM 1
Share

And Im edited script again :)

Just add again:

 Target.transform.position = specificVector;
avatar image chariot · Feb 11, 2015 at 03:44 PM 1
Share

cause u deleted specificVector ;) and your prefab instantiates in (0,0,0) just copy my script from first comment, i guess now it can works well

Show more comments

1 Reply

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

Answer by chariot · Feb 11, 2015 at 03:56 PM

 using UnityEngine;
   using System.Collections;
   
   public class ObjectToFollowMouse : MonoBehaviour
   {
       
       public GameObject Target;
       public GameObject ActualPrefab;
       public Vector3 specificVector;
       
       RaycastHit hit;
       
       private float raycastLength = 1000;
       
       void Start()
       {
           
       }
       
       
       void Update()
       {    
           Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
           
           //Makes the 'Fake' Prefab follow the mouse.
           if(Physics.Raycast(ray, out hit, raycastLength))
           {
               Debug.Log(hit.collider.name);
               
               if(hit.collider.name == "Terrain")
               {
                   specificVector.Set(hit.point.x, hit.collider.transform.position.y, hit.point.z);
                   Target.transform.position = specificVector;
               }
           }
           
           //Instantiates Actual Building Prefab
           if(Input.GetMouseButtonDown(1))
           {
               GameObject TargetObj = Instantiate(ActualPrefab, specificVector, Quaternion.identity) as GameObject;
               TargetObj.name = "Target Instantiated";
               
               //Destroys the Fake !
               Destroy(gameObject);
           }
       }
   }

Answered script

Comment
Add comment · 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

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

20 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

Related Questions

How should i add raycast to multiple game objects??? 0 Answers

Use raycast to see if something else than terrain is under 1 Answer

[SOLVED] Only instantiating once 1 Answer

Keep random Instantiation from spawning ontop of other objects 1 Answer

Adjust the terrain to a mesh 0 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