- Home /
Raycast only working once
I have a scene where I have several different shapes. Attached to an object is a script containing the raycast stuff. When the ray intersects an object of a certain shape, I want it do show the shape that it has intersected.
For some reason, It will work the first time but as soon as I move off a shape, the raycast seems to do nothing and it wont recognize that its hit again the second time. It has me stumped cause I have used raycasts before and not run into this problem. It appears that it only runs the update once rather than continuously.
Help greatly appreciated
using UnityEngine;
using System.Collections;
public class RaycastTriangle : MonoBehaviour {
// Use this for initialization
void Start () {
}
void Update() {
RaycastHit hit;
Vector3 up = transform.TransformDirection(Vector3.up);
if (Physics.Raycast(transform.position, up, out hit))
{
if (hit.collider.gameObject.CompareTag("Triangle"))
{
print("Triangle");
}
else if (hit.collider.gameObject.CompareTag("Circle"))
{
print("Square");
}
else if (hit.collider.gameObject.CompareTag("Circle"))
{
print("Circle");
}
else
{
print("No Shape");
}
}
Debug.DrawRay(transform.position, up, Color.green);
}
}
Is the object that this script attached to moving or rotating at all?
The script is attached to an empty game object that is parented to a cube that moves around. each side of the cube has a raycast so that whenever it moves, it can pick up objects no matter what side of the cube they are on
I just got email notice: "8r3nd4n has accepted an answer by 8r3nd4n for Raycast only working once"
Lets see.. You had a question, and you answered it by yourself after two days, with two identical answers. It took over three years to make up your $$anonymous$$d, which one is acceptable :)
Answer by 8r3nd4n · Nov 24, 2011 at 08:46 AM
Damn it I was pulling my hair out for so long and as usual the answer is so simple I can only laugh at my stupidity.
I was always checking the console for the hit detection but had the 'collapse' button toggled so it would only show it the first time.
ha ha ha
Dude, you saved my $$anonymous$$d! xDDD I was having the same issue... Thanks! :D
I had this problem before and forgot all about it; now I have fallen for it again! Thanks 8r3nd4n!
you're not the only one ..probably happened all of us at least ones :)
hahaha , just logging in to say that i had the same 'bug'. rofl.
thanks for saving time !
You saved my time ...sometimes happens with programmers ...... : )
Answer by 8r3nd4n · Nov 24, 2011 at 08:46 AM
Damn it I was pulling my hair out for so long and as usual the answer is so simple I can only laugh at my stupidity.
I was always checking the console for the hit detection but had the 'collapse' button toggled so it would only show it the first time.
ha ha ha
Your answer
Follow this Question
Related Questions
How to hit two objects with only one shot (raycast) 0 Answers
Need help with Third person shooter 0 Answers
Ray from enemy to player is always a bit off (Image) 1 Answer
RaycastHit.distance 1 Answer