Get coordinates of mouse-click on plane
I'm basically a newbie with Unity. I've created some simple projects from tutorials but only have basic understanding. I have a plane in 3D space. I want to click on the plane with the mouse and get the coordinates for that click (i.e. whereabouts on the plane I clicked) . Currently I can only get the world coordinates on-click. I tried getting the position of the plane in world space so that I can convert my mouse click coordinates to plane coordinates but this doesn't do what I expected and actually gives me the coordinates on plane relative to the transform associate with my player script. I feel like there may be a better way to do this anyway.
Here is my simplified PlayerMovement script:
public class PlayerMovement : MonoBehaviour {
 // Use this for initialization
 void Start () {
     plane = GetComponent(typeof(MeshFilter)) as MeshFilter;
 }
 
  void Update() {
     if (Input.GetMouseButtonDown (0)) {
         Debug.Log ("mouseDown = " + Input.mousePosition.x + " " + Input.mousePosition.y);
         Debug.Log ("position in plane = " + plane.transform.position);
     }
 }
 
               }
I'd really appreciate any pointers here. I imagine it's simple enough when you know how. I have looked at quite a few articles and pieces of documentation but I just can't find a solution. Thanks in advance.
Well this is an old post but it might help someone else now. What i use for 2D stuff is this:
worldCoordinates = Camera.main.ScreenToWorldPoint(Input.mousePosition); { Debug.Log(worldCoordinates.x); Debug.Log(worldCoordinates.y); }
I dont know if its the best approach but it works like a charm.
Answer by SteenPetersen · Jul 30, 2017 at 03:28 PM
You can try this:
     private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             clicked();
         }
     }
 
     void Clicked()
     {
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hit = new RaycastHit();
 
         if (Physics.Raycast (ray, out hit))
         {
             Debug.Log(hit.collider.gameObject.name);
         }
     }
 
               let me know if you have questions.
Hi, thankyou SteenPetersen, however i tried a raycasting solution similar to this previously and I always get the error: NullReferenceException: Object reference not set to an instance of an object Player$$anonymous$$ovement.Clicked () (at Assets/Scripts/Player$$anonymous$$ovement.cs:15) Player$$anonymous$$ovement.Update () (at Assets/Scripts/Player$$anonymous$$ovement.cs:10)
which is the line: var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
I have had exactly the same problem previously. Am I doing something wrong with my camera or something like this?
can you post the relevant script for me please?
Yes of course....
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
 private void Update() {
      if (Input.Get$$anonymous$$ouseButtonDown(0))
      {
          Clicked();
      }
  }
 
  void Clicked() {
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
      RaycastHit hit = new RaycastHit();
 
      if (Physics.Raycast (ray, out hit))
      {
          Debug.Log(hit.collider.gameObject.name);
      }
  }
 
                    }
Your answer
 
             Follow this Question
Related Questions
C# Mathf.Round Not Rounding Input.mouseposition 0 Answers
How to set player position to a specific position? 1 Answer
Remember position for a simple player controller (Left/Right)? 0 Answers
Shooting a projectile with Isometric View 0 Answers
How to prevent the player to move beyond certain x-position value 3 Answers