- Home /
Using Spherecast to identify objects?
So, the cubes with color are the NPCs, and when the game first starts, the cubes starts to rotate. When the player presses the screen, the cubes stop spinning. After stop spinning, I want to identify who is most far from the white cubes (basically it is like musical chair, and those color cubes are going to "sit" on those white cubes, and the one that is furthest from the white chair cannot "sit")
I tried using spherecast, but it somehow doesn't work. Here is my code:
void Update ()
{
RaycastHit hit = new RaycastHit ();
if (Physics.SphereCast (transform.position, 100.0f, transform.forward, out hit))
{
if (hit.collider.tag == "Chair")
print (this.name + "Hit!");
}
}
There is nothing wrong with this code. I'm guessing its the way you fire the raycast.
Add more diagnostics code: print the object name & object tag if the collision occurs.
You could also look into using a Gizmo to visualise what it going on.
Answer by Mark Gossage · Jun 23, 2015 at 10:37 AM
This is a 2D game, but you are using the 3D physics engine to do the work. Switch to the 2D physics engine Physics2D & instead of using transform.forward, use the direction you want to access. I seem to remember that in 2D mode, transform.forward might be into the screen, in which case you will not hit anything.