I have a scene where I have several different shapes. Attached to an object that is moved around by the player 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 Code (csharp): 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); } }
Code (csharp): else if (hit.collider.gameObject.CompareTag("Circle")) { print("Square"); } There is a mistake here it is not Circle but Square
Thanks guys but it will still only work once even with FixedUpdate And thanks for pointing out the typo Tazadar but it still didnt affect the behaviour
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
hahaha! I have been trying to figure this out on my own program for hours now. And I just read this and I was doing the same thing. Glad I stopped on this article. Thanks, 8r3nd4n for figuring this out. And the laugh at myself.