- Home /
bug? with raycasts and booleans
I'm not getting a match when the 2 colors align vertically, and only the top one doesn't work, here is a video of the issue:
https://www.youtube.com/watch?v=r-6cv15d9SU&feature=youtu.be
I have debugged the rays and this is the result of the rays, they ignore the layer the object they are inside in of, when dragged outside this happens
Then when next to eachother:
Here is the code for it, I broke it all up attempting to find what was going on:
using UnityEngine;
using System.Collections;
public class Cylinder : MonoBehaviour {
public bool cylinderRay;
public LayerMask layerToIgnore;
void Start () {
Color objectColor = renderer.material.GetColor("_Color");
}
void Update () {
Cylinder otherObject;
Color objectColor = renderer.material.GetColor("_Color");
RaycastHit hit;
Vector3 angle0 = transform.TransformDirection(Vector3.up);
cylinderRay = Physics.Raycast(transform.position, angle0, out hit, 1);
Debug.DrawRay (transform.position, angle0 * 1, Color.blue);
if(cylinderRay){
if(hit.transform.tag == "Cylinder" && !hit.transform.parent.GetComponent<Molecule>().isFalling){
Color otherObjectColor = hit.collider.renderer.material.GetColor("_Color");
Molecule molecule = hit.transform.parent.GetComponent<Molecule>();
if(objectColor != otherObjectColor){
print ("this object isn't the same color as me");
if (objectColor == Color.green){
transform.parent.GetComponent<Molecule>().GreenCylMatch = false;
}
if(objectColor == Color.blue){
transform.parent.GetComponent<Molecule>().BlueCylMatch = false;
}
if(objectColor == Color.red){
transform.parent.GetComponent<Molecule>().RedCylMatch = false;
}
}
if(transform.parent.GetComponent<Molecule>().hasMatches){
if(objectColor == otherObjectColor){
molecule.hasMatches = true;
}
}
if(transform.parent.GetComponent<Molecule>().hasMatches
|| hit.transform.parent.GetComponent<Molecule>().hasMatches){
if(objectColor == otherObjectColor){
print ("other object has my color");
molecule.hasMatches = true;
}
transform.parent.GetComponent<Molecule>().matchFound();
}
if(objectColor == otherObjectColor){
if(objectColor == Color.green && otherObjectColor == Color.green){
print ("other object is my color and im green");
transform.parent.GetComponent<Molecule>().GreenCylMatch = true;
hit.transform.parent.GetComponent<Molecule>().GreenCylMatch = true;
}
if(objectColor == Color.blue && otherObjectColor == Color.blue){
print ("other object is my color and im blue");
transform.parent.GetComponent<Molecule>().BlueCylMatch = true;
hit.transform.parent.GetComponent<Molecule>().BlueCylMatch = true;
}
if(objectColor == Color.red && otherObjectColor == Color.red){
print ("other object is my color and im red");
transform.parent.GetComponent<Molecule>().RedCylMatch = true;
hit.transform.parent.GetComponent<Molecule>().RedCylMatch = true;
}
}
}
} else {
print ("ray not hitting anything or incorrect object");
if (objectColor == Color.green){
transform.parent.GetComponent<Molecule>().GreenCylMatch = false;
}
if(objectColor == Color.blue){
transform.parent.GetComponent<Molecule>().BlueCylMatch = false;
}
if(objectColor == Color.red){
transform.parent.GetComponent<Molecule>().RedCylMatch = false;
}
}
}
}
The print it gives me is "Not hitting cylinder or other object is falling", the other object is not falling and that boolean is false, what could be wrong here or am I casting incorrectly? The strangest thing is that not all of them do it, but if they do it it's nearly always the same row and always vertically down, the upper cylinder.
Bump, I have changed some of my code but it still occurs sometimes.
I'm having problems to understand your problem. Please provide a summary: What should happen? And when? What happens ins$$anonymous$$d?
At all times, when the 2 cylinders are connected they should both light up as that means that they both have found a match with another blue cylinder, sometimes not always the cylinder that is on top will not get the match but the one on the bottom does, when dragged outside the black ball it seems to occur at all times however, the raycast ignores the layer the black ball is on.
Answer by unimechanic · Sep 30, 2014 at 04:24 PM
Make sure that you are not transforming static colliders, that could corrupt the physics scene and make Raycast to return wrong results. See the Static Colliders section:
http://docs.unity3d.com/430/Documentation/Components/DynamicsGroupOverview.html
Your answer
Follow this Question
Related Questions
How to determine direction in order to compare it to raycast results 0 Answers
Line Renderer not showing 0 Answers
Physics.Raycast not hit sometimes 0 Answers
Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers
Can else statements be nested inside an else statement? 1 Answer