Lets say I do a raycast like this:
var mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
var rayhitlocation : RaycastHit;
layerMask = ~(1 << 9);//invert bitshift to exclude only layer 9 from raycast
Physics.Raycast(mouseray, rayhitlocation, Mathf.Infinity, layerMask);
How would I assign a GameObject in my scene from whatever the raycast hit? So far, I think it’s purely syntax that I am getting wrong, but I’m not sure. I’ve also tried using the “collider.Raycast()” function like this:
if (collider.Raycast(mray, rayhitlocation, Mathf.Infinity)) {
selected = true;//set rayhit object to selected
Unitmanager.unit = this.gameObject;
renderer.material.color = Color(.13,.97,.20,1);
//rigidhit = target.collider.attachedRigidbody;
}
I had another script with a static GameObject variable (script called Unitmanager), that I tried to assign the collider from the raycast to be the gameObject that it hit, but again it didn’t work…
So how can I grab a specific GameObject from a raycast? Should I use the collider.Raycast or the physics.Raycast as well?