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
2
Question by Lisk · Dec 11, 2013 at 06:31 PM ·

Detect mouse click coordinates on terrain in C#

I'd like to detect the coordinates (x, y) on my terrain when the mouse is clicked.

I've tried searching Unity answers & Google for answers, but wasn't able to find something that worked. (Either it was in JS, or for an older version of Unity, or contained project-specific code that I don't have access to).

The context is I'm making a 2D isometric game and want to be able to right-click to move the character to a specific point.

I don't have a good conceptual grasp on Raycasts/Raycast semantics, so I'd prefer a specific code snippet if possible, rather than pseudocode.

Thanks!

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

2 Replies

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

Answer by robertbu · Dec 11, 2013 at 06:40 PM

The following code will move an object to the hit point on a terrain. To use:

  • Create a terrain

  • Create an object to place on the terrain

  • Attach the script to the object to place

  • Select the object to place in the hierarchy. Drag and drop the terrain onto the 'goTerrain' veriable in the inspector

  • Hit play and right mouse click on the terrain at the position you want to move the object.


    using UnityEngine; using System.Collections;

    public class TerrainHit : MonoBehaviour { public GameObject goTerrain;

       void Update() {
             if (Input.GetMouseButtonDown (1)) {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 if (goTerrain.collider.Raycast (ray, out hit, Mathf.Infinity)) {
                     transform.position = hit.point;
                 }
             }
         }
     }
    
    

Note this code is a bit different that most of the Raycast() code you will find on UA. Most code uses Physics.Raycast(). This code use Collider.Raycast() so that it cast only against the terrain (which is what you indicated you needed in your question).

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 Lisk · Dec 11, 2013 at 07:20 PM 0
Share

Thanks for the prompt & detailed response!

avatar image ArtOfWarfare · Dec 20, 2015 at 06:05 PM 1
Share

Object.collider (and with it, Terrain.collider, which just inherited the property) is deprecated as of 5.3.0. Use GetComponent<Collider>(), ins$$anonymous$$d.

avatar image kinyodas · Jan 05, 2016 at 10:58 PM 0
Share

Question: I attempted the following modified code, but I am having issue:

void Update () { if (Input.Get$$anonymous$$ouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (GetComponent().Raycast(ray, out hit, $$anonymous$$athf.Infinity)) { transform.position = hit.point; } } }

This seems to pause the game when clicking the left mouse button and does not return a hit.point value; any help is appreciated. Also, in absence of the goTerrain variable, I see no substitute.

avatar image ArtOfWarfare kinyodas · Jan 05, 2016 at 11:11 PM 0
Share

Give yourself a goTerrain variable... There's no reason not to have one. You also need RaycastHit hit; somewhere in your code. Then you can do if (goTerrain.GetComponent<Collider>().Raycast(Ray, out hit, $$anonymous$$athf.Infinity)

avatar image kinyodas · Jan 07, 2016 at 10:25 PM 0
Share

ArtOfWarfare,

I actually didn't see the top of the code because of the formatting. I also noted the depreciation.

Thank you tons for your timely help!

-$$anonymous$$

avatar image astinog · Feb 05, 2016 at 08:57 PM 0
Share

And what if I want to detect where to go in every "terrain" type area? Can I use layers?

avatar image
0

Answer by iamarugin · Jan 04, 2017 at 08:49 AM

 public class SomeClass : MonoBehaviour, IPointerClickHandler {
     public void OnPointerClick(PointerEventData eventData) {    
        transform.position = eventData.pointerCurrentRaycast.worldPosition;    
     }
 }

You should also attach Physics Raycaster component to your camera.

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

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

Is there a way to debug the iOS simulator with MonoDevelop? 1 Answer

Continuing problems with windows 8.1 0 Answers

Will PlayerPrefs work on PS3? 0 Answers

How to Destory a Gameobject in C# after 3 seconds? 3 Answers

How do I get a line renderer to draw over sprites? 2 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