- Home /
Question by
faraz_zaidi22 · Jun 26, 2019 at 11:01 AM ·
touchcollision detectionmeshcollidermeshrenderertouchphase
How to detect touch on a 3d object?
I a beginner and i am currently trying to detect touch on a 3d object but so far i have failed miserably. I have tried on trigger function and many other stuff but nothing seems to work. The touch or the line drawn with touch is acting behind the screen for some reason.
and this is happening when i hit play
Here is the code for drawing a line with touch
void Start ()
{
planeobj = new Plane(Camera.main.transform.forward * -1, this.transform.position);
}
// Update is called once per frame
void Update()
{
Touch();
}
public void Touch()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began || Input.GetMouseButtonDown(0))
{
theTrail = (GameObject)Instantiate(drawline, this.transform.position, Quaternion.identity);
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float _distance;
if (planeobj.Raycast(mouseRay, out _distance))
{
startPos = mouseRay.GetPoint(_distance);
}
}
else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetMouseButton(0))
{
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float _distance;
if (planeobj.Raycast(mouseRay, out _distance))
{
theTrail.transform.position = mouseRay.GetPoint(_distance);
}
}
else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetMouseButtonUp(0))
{
if (Vector3.Distance(theTrail.transform.position, startPos) < 0.1)
{
Destroy(theTrail);
}
}
and here is the code for detecting collision
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
Debug.Log("hellooooo");
}
}
[2]: https://ibb.co/bXLHF2c
Comment