This question was
closed Mar 03, 2017 at 02:41 PM by
PresidentPorpoise for the following reason:
Other
Question by
PresidentPorpoise · Dec 07, 2016 at 11:55 PM ·
raycastprogrammingraycasthittags
Issues with detecting collision with objects that have certain tags using Raycast
Hello, I am trying to use Raycast to change the color of a crosshair when the player looks at an object tagged "interactable," and I successfully did but with a big issue. When aiming the player (and ultimately the ray) toward an object tagged "interactable" and has colliders, the crosshair only turns green when looking at certain spots on the object. I believe the ray may be only getting info from the endpoint instead of anything between the ray and the endpoint, but I do not know why. How can I fix this?
My scripts:
Swaps crosshair to a green version
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayViewer : MonoBehaviour {
public float range = 50f;
private Camera fpsCam;
void Start()
{
fpsCam = GetComponent<Camera>();
}
// Update is called once per frame
void Update ()
{
Vector3 lineOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
Debug.DrawRay(lineOrigin, fpsCam.transform.forward * range, Color.green);
}
}
Creates ray:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayViewer : MonoBehaviour {
public float range = 50f;
private Camera fpsCam;
void Start()
{
fpsCam = GetComponent<Camera>();
}
// Update is called once per frame
void Update ()
{
Vector3 lineOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
Debug.DrawRay(lineOrigin, fpsCam.transform.forward * range, Color.green);
}
}
Comment