Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by raffaga · Sep 18, 2015 at 08:09 PM · raycastmousebuildingplacement

Placing an Object at mouse position on a Terrain

I am traying to place an object based on the mouse position, before setting the object final position, I want the player to be able to move the object around the map to choose a location.

I try using a Raycast, but it sends the object to the center of the terrain, instead of the exact position the ray is hitting.

Does ray cast detects exact position in a Terrain? or does it only detecs the transfrom of the object a a whole?

any ideas what am i doing wrong?

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 namespace UnityStandardAssets.Characters.FirstPerson
 {
     public class ObjectPlacer : MonoBehaviour
     {
         public GameObject smelterdummy;
 
         private ActionBar actionBar;
         private InventoryV2 inventoryV2;
         private PlayerHP playerHP;
         private Actions actions;
         private AudioSource collectSound;
         private GameObject smelterGO;
         private RaycastHit hit;
         private bool once = true;
 
         // Use this for initialization
         void Start()
         {
             collectSound = GameObject.FindGameObjectWithTag("TakeSound").GetComponent<AudioSource>();
             actionBar = GameObject.FindGameObjectWithTag("ActionBar").GetComponent<ActionBar>();
             inventoryV2 = GameObject.FindGameObjectWithTag("GUI").GetComponent<InventoryV2>();
             playerHP = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHP>();
             actions = GameObject.FindGameObjectWithTag("AM").GetComponent<Actions>();
         }
 
         // Update is called once per frame
         void Update()
         {
             if (smelterGO != null)
             {
                 Ray placefinder = Camera.main.ScreenPointToRay(Input.mousePosition);
                 if(Physics.Raycast(placefinder, out hit,  300))
                 {
                     smelterGO.transform.position = hit.transform.position;
                     smelterGO.transform.position = new Vector3(smelterGO.transform.position.x, Terrain.activeTerrain.SampleHeight(smelterGO.transform.position), smelterGO.transform.position.z);
                 }
             }
             Placement();
         }
 
         public void OnMouseOver()
         {
             
         }
 
         void Placement()
         {
             if (actionBar.actionBarSlot[actionBar.abPosition] == 23)
             {
                 if (once)
                 {
                     once = false;
                     smelterGO = Instantiate(smelterdummy) as GameObject;
                 }
                 actions.disableOn = true;
             }
             else
             {
                 Debug.Log("HEre!!");
                 actions.disableOn = false;
                 Destroy(smelterGO);
                 once = true;
             }
         }
     }
 }
Comment
Add comment
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

3 Replies

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

Answer by Rostam24 · Sep 18, 2015 at 09:10 PM

You are right, if you get the position of the transform of the hit, you will get the pivot point of the transform. If you want the hit point, use raycasthit.point instead: http://docs.unity3d.com/ScriptReference/RaycastHit-point.html

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 raffaga · Sep 18, 2015 at 09:25 PM 0
Share

You were absolutly right, but now my object is moving in clycles from the hit.point into the camera... over and over again... any ideas why? @Rostam24

avatar image
0

Answer by raffaga · Sep 19, 2015 at 05:26 AM

In case someone is having a similar issue... I have a few empty objects with large colliders to limit spawn resorces in certain areas of my map. My object was doing that movement because the RayCast was colliding with this huge colliders. I added a Layer mask to my ray, so it would ignore everything but the terrain. it worked like a charm! Thanks ;)))

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
avatar image
0

Answer by GXMark · Jan 24, 2017 at 02:45 PM

If you want to spawn an object on the terrain at some spawn distance in front of the camera only taking into account the camera y angle. Then you could write this.

 float _spawnDistance = 30f;
 float _terrainYOffset = 300f
 
 GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere)
 
 // Calculate the spawn position
 Vector3 spawnPosition = Camera.main.transform.position + Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y, Vector3.up) * Vector3.forward * _spawnDistance;
 
 // Add an offset to y to cater for terrain slope and object heights
 spawnPosition = new Vector3(spawnPosition.x, spawnPosition.y + _terrainYOffset, spawnPosition.z);
 
 // Raycast downwards to find terrain
 RaycastHit hitPoint = new RaycastHit();
 Physics.Linecast(spawnPosition, new Vector3(spawnPosition.x, spawnPosition.y - 1000, spawnPosition.z), out hitPoint);
 
 // Place the object in front of camera
 obj.transform.position = hitPoint.point + new Vector3(obj.transform.position.x, obj.transform.position.y + (obj.transform.localScale.y * 0.5f), obj.transform.position.z);

// If you want the spawned object to face the camera angle obj.transform.eulerAngles = new Vector3(0f, Camera.main.transform.eulerAngles.y, 0f);

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.

Follow this Question

Answers Answers and Comments

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I use Input.GetButton, instead of OnMouse() 1 Answer

How to Place Model FULLY on Top of Plane ? 0 Answers

Unity 2D tile based building simulation(Building problems) 0 Answers

how to stop mouse from teleporting or detect better, so that i can detect if a swipe of mouse was over object 0 Answers

Empty scene : get mouse position in scene 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