Question by
Esteem · Oct 04, 2016 at 06:47 PM ·
collidermouseclickworldspacecollider.raycast
World space position on mouseclick (several positions on one collider)
Hi, I've got a question about actual world space position when I use this script on the game object I.ve made. (pic related) when I click on any point on this object, the script will always spit out the location of the game object's collider, but I want to pinpoint the exact position of my mouseclick on this one collider. Is it possible?
Thank you
void Update(){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, Mathf.Infinity, floorMask)){
if (Input.GetMouseButton(1)){
distance = cam.ScreenToWorldPoint(Input.mousePosition);
print (distance);
}
}
}
untitled.png
(4.3 kB)
Comment
Best Answer
Answer by doublemax · Oct 04, 2016 at 06:51 PM
Use an overload of Physics.Raycast that takes a "out RaycastHit hitInfo" as parameter. Then you can access the exact hit point with hitInfo.point
Oh, thank you!
using UnityEngine;
using System.Collections;
public class CameraScript : $$anonymous$$onoBehaviour {
Vector3 distance;
RaycastHit hit;
int floor$$anonymous$$ask;
int floorLayer = 31;
void Awake () {
floor$$anonymous$$ask = 1 << floorLayer;
}
void Update(){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity, floor$$anonymous$$ask)){
if (Input.Get$$anonymous$$ouseButton(1)){
distance = hit.point;
print (distance);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612094013im_/https://answers.unity.com/themes/thub/images/avi.jpg)