- Home /
Raycast Collider Tag not returning correct result
Hey there, just a small problem I am having raycasting in Unity. I want to play a video after clicking a gameObject tagged "PlayButton1" which when clicked will play/pause the video. I have the code almost working, in that, if I add a semi colon after the hit.transform.tag line it will play/pause the video, but when clicked anywhere, not clicking on the specific object I want.
Really can't figure this out and don't know ehere to go from here. Thanks in advance!
var movTexture : MovieTexture;
function Update () {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, hit, 100))
{
if (hit.collider.tag == "PlayButton1")
{
if (movTexture.isPlaying) {
movTexture.Pause();
}
else {
movTexture.Play();
}
}
}
}
}
Answer by Calum-McManus · Mar 10, 2014 at 02:33 PM
Try this, I'm sure you simply misplaced a section of your code.
var movTexture : MovieTexture;
function Update () {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, hit, 100))
{
if (hit.collider.tag == "PlayButton1")
{
if (movTexture.isPlaying)
{
movTexture.Pause();
}
else
{
movTexture.Play();
}
}
}
}
}
If this doesn't work try adding in a Debug.Log so that you can make sure it's hitting the right tag and make sure the collider on the button is not set to trigger.
Note: if it still dosn't work try chaing "ScreenPointToRay" to "ViewportPointToRay"