how make raycast detect in object for crosshair
hi i have a problem i dont now how to make the ray cast detect a spesific object yet i can make a ui image enable and disable when detect anything or not but i just whant it to do it for one sesific object sorry for mi bas sepling im mexican xD
this is what im jusing
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class raycast : MonoBehaviour
{
public Image action;
public Image nonaction;
public GameObject note1;
void FixedUpdate()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, transform.forward, Color.green, 0);
if (Physics.Raycast(transform.position, fwd, 1))
action.enabled = true;
else
{
print("there is nothing in front");
action.enabled = false;
}
}
}
HELP PLSSSS :c
hola, prefiero responder en español para poder entender lo que respondas a su vez,
posibles errores, me baso en la suposicion que este script esta asociado a la camara:
1) transform.TransformDirection()
transforma cooredenadas de LocalSpace a WorldSpace pero Vector3.forward
devuelve de por si coordenadas en WorldSpace, asi que estas realizando una conversion de más Vector3 prueba simplemente fwd = Vector3.forward;
2) Physics.Raycast()
toma como tercer parametro la distancia maxima que alcanza a detectar, tu rayo no detectara objetos que esten mas lejos de 1 unidad de distancia Unity
3) para que raycast detecte a los objetos estos deben poseer un componente de tipo collider, aunque es muy obvio quizas se te ha olvidado agregarselo a algún objeto.
espero que alguna sea de ayuda.
the same answer in english, if someone want to read it.
possible mistakes, I think to this script is asociated to the camera:
1) transform.TransformDirection()
convert coords from LocalSpace to WorldSpace, but Vector3.forward
already returns coords in WorldSpace so you make a erroneous conversion, just try fwd = Vector3.forward;
2) the third parameter of Physics.Raycast()
is the $$anonymous$$axDistance of the raycast, so your ray just will detect objects to are more close than 1 unit of distance.
3) raycast only detects the object if it have asociated a collider component, maybe is obvious but maybe you forget to put it on any object.
Your answer
Follow this Question
Related Questions
Raycast detect in object (problem) 0 Answers
Why am I getting a Null Reference Exception? 0 Answers
Raycast is offsetting object 1 Answer
Follow crosshair trail 0 Answers
Is there a better way to check if a player is looking at a certain object? 0 Answers