I am working on some character AI, and im trying to make it to where the enemy “looks” for the player by sending out rays from the head (gameobject), and returns true if it hits the Team1 layerd collider. Can anyone help get me started? Thanks!
I already looked at that, but its very confusing for me
if (rayname.collider.tag == “Tag”)
{}
I think it was .collider, if not, it’s something similar.
the layermask defines which layers are included in the search.
For AI it might be better to find all other elements in range and then use raycasting to get concrete information about them.
can you post an example snippet using the layermask?
collider.layer?
What I am confused on is how to actually use the collider.layer
To find the correct layermask you have to find the number of your layer. Use Layers → Edit Layers. There you can see Builtin Layer 0 -7 and User Layer 8 - 31. The correct layer mask is 1 << YourLayerNumber so if your layer is User layer 8 you have to put 1<<8 into your layer mask variable.
Some examples using raycasting can be found here
Unity - Manual: Layers.
If you want to look up what 1 << 8 means search for “bitwise operations”.
Actually you don’t need any of these 1<<8 thingys. Just use collider.later == 1 or “1”, forgot which one.
I actually think im starting to understand this!
Heres what I have so far
var layerMask = 1 << 8;
function Update () {
var fwd : Vector3 = transform.TransformDirection(Vector3(0, 0, -1));
if (Physics.Raycast (transform.position, fwd, Mathf.Infinity, layerMask)) {
transform.renderer.material.color = Color.yellow;
}
}
but when my Character controller (on layer 8) moves in front of (the z is -1 due to some problems with the model not facing the right way) dude with this script, or anywhere aroiund him, nothing happens. What did I do wrong this time lol
anyone?
Does your other model have any kind of collision information attached. Physics.Raycast only works if there are colliders attached.
Are the targets correctly assigned to layer 8 ( just to be sure).
Have you tried to use Debug.Log to output a message? Maybe just the color isn’t changed.
If nothing works try to put any object at (transform.position + fwd * 5) (updated inside this function) to see if you are getting the correct direction result.
I got it to work, but is there anyway i can stop the ray when it hits an object on a certain layer?