Raycasts: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Fabian (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „Raycasts dienen dafür, eine unsichtbare Achse von einem bestimmten Punkt in die Welt zu schlagen. In VR sind sie nützlich, um den Blicken der Benutzerin zu f…“) |
Fabian (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
Raycasts dienen dafür, eine unsichtbare Achse von einem bestimmten Punkt in die Welt zu schlagen. In VR sind sie nützlich, um den Blicken der Benutzerin zu folgen | Raycasts dienen dafür, eine unsichtbare Achse von einem bestimmten Punkt in die Welt zu schlagen. In VR sind sie nützlich, um den Blicken der Benutzerin zu folgen. In AR können sie benutzt werden um eine Achse von der Kamera des Benutzer in die reale Welt zu projizieren, reale Projekte zu berühren und auf dem Tablet interagierbar zu machen. | ||
using System.Collections; | |||
using System.Collections.Generic; | |||
using UnityEngine; | |||
using UnityEngine.UI; | |||
public class Hit : MonoBehaviour | |||
{ | |||
public SendOSC SendOSC; | |||
public Camera playerCamera; | |||
public GameObject wuerfel; | |||
public Text textDebug ; | |||
public Text touchedObjectText; | |||
public GameObject hitGameObject; | |||
public GameObject rayTest; | |||
int ledStatus = 0; | |||
int hitStatus = 0; | |||
// Start is called before the first frame update | |||
// Update is called once per frame | |||
void Update() | |||
{ if (Input.touchCount >0) | |||
{ textDebug.text = "TOUCHED"; | |||
PerformRaycast(); | |||
} | |||
else | |||
{ | |||
textDebug.text = "NO TOUCH"; | |||
hitStatus = 0; | |||
} | |||
if (Input.touchCount >0 && Input.GetTouch(0).phase==TouchPhase.Began) | |||
{ | |||
textDebug.text = "RAYCAST"; | |||
PerformRaycast(); | |||
} | |||
} | |||
void PerformRaycast() | |||
{ | |||
Vector3 ray = playerCamera.ScreenToWorldPoint(Input.GetTouch(0).position); | |||
Ray myRay = playerCamera.ScreenPointToRay(Input.GetTouch(0).position); | |||
RaycastHit hit; | |||
touchedObjectText.text = ray.ToString(); | |||
rayTest.transform.position = myRay.origin; | |||
rayTest.transform.LookAt(myRay.direction); | |||
//if (Physics.Raycast(ray, playerCamera.transform.forward, out hit, Mathf.Infinity)) | |||
//macht ray bei Camerapunkt | |||
if (Physics.Raycast(myRay.origin, myRay.direction, out hit, Mathf.Infinity)) | |||
{ | |||
touchedObjectText.text = "HIT!"; | |||
Debug.DrawRay(ray, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow); | |||
hitGameObject = hit.collider.gameObject; | |||
touchedObjectText.text =hitGameObject.name.ToString(); | |||
if (hitGameObject.name == "ToBeHit" && hitStatus == 0) { | |||
SendOSC.OscMsgStuhl(ledStatus); | |||
hitStatus = 1; | |||
if (ledStatus==0) { | |||
ledStatus = 1; | |||
} | |||
else { ledStatus = 0; } | |||
textDebug.text = "OSC"; | |||
} | |||
// Debug log this: hit.collider.gameObject.name; | |||
//zum material: | |||
//hitGameObject.GetComponent<Renderer>().material; | |||
} | |||
else | |||
{ | |||
//no hit! i repeat, no hit!! | |||
//touchedObjectText.text = "NO HIT"; | |||
} | |||
} | |||
} | |||
Version vom 1. April 2020, 18:38 Uhr
Raycasts dienen dafür, eine unsichtbare Achse von einem bestimmten Punkt in die Welt zu schlagen. In VR sind sie nützlich, um den Blicken der Benutzerin zu folgen. In AR können sie benutzt werden um eine Achse von der Kamera des Benutzer in die reale Welt zu projizieren, reale Projekte zu berühren und auf dem Tablet interagierbar zu machen.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Hit : MonoBehaviour {
public SendOSC SendOSC; public Camera playerCamera; public GameObject wuerfel; public Text textDebug ; public Text touchedObjectText; public GameObject hitGameObject; public GameObject rayTest; int ledStatus = 0; int hitStatus = 0;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{ if (Input.touchCount >0)
{ textDebug.text = "TOUCHED";
PerformRaycast();
}
else
{
textDebug.text = "NO TOUCH";
hitStatus = 0;
}
if (Input.touchCount >0 && Input.GetTouch(0).phase==TouchPhase.Began)
{
textDebug.text = "RAYCAST";
PerformRaycast();
}
}
void PerformRaycast()
{
Vector3 ray = playerCamera.ScreenToWorldPoint(Input.GetTouch(0).position);
Ray myRay = playerCamera.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit;
touchedObjectText.text = ray.ToString();
rayTest.transform.position = myRay.origin;
rayTest.transform.LookAt(myRay.direction);
//if (Physics.Raycast(ray, playerCamera.transform.forward, out hit, Mathf.Infinity))
//macht ray bei Camerapunkt
if (Physics.Raycast(myRay.origin, myRay.direction, out hit, Mathf.Infinity))
{
touchedObjectText.text = "HIT!";
Debug.DrawRay(ray, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
hitGameObject = hit.collider.gameObject;
touchedObjectText.text =hitGameObject.name.ToString();
if (hitGameObject.name == "ToBeHit" && hitStatus == 0) {
SendOSC.OscMsgStuhl(ledStatus);
hitStatus = 1;
if (ledStatus==0) {
ledStatus = 1;
}
else { ledStatus = 0; }
textDebug.text = "OSC";
}
// Debug log this: hit.collider.gameObject.name;
//zum material:
//hitGameObject.GetComponent<Renderer>().material;
}
else
{
//no hit! i repeat, no hit!!
//touchedObjectText.text = "NO HIT";
}
}
}