- Home /
 
Raycasting on Moving Colliders
I perform raycasting on units that are moving in my 2D RTS-style game in order to select it and return information about it. However, I cannot get the raycasting to perform correctly while the unit is moving.
The movement takes place in the FixedUpdate function on the individual unit itself (i.e. there is a Movement script attached to the unit that performs the moving). The Raycast for selecting is done in a Manager script, and is also in a FixedUpdate function. However, no matter what type of update function I put the Raycast in, it does not detect the unit while it's moving.
The units only contain box colliders (no rigidbodies), and are moved using transform.Translate().
How can I get raycast to work on colliders that are moving?
Relevant code:
Movement function:
     def FixedUpdate():
         if not path:
             return
         
         if currentWaypoint >= path.vectorPath.Count:
             Debug.Log("End of path reached.")
             return
             
         dir as Vector3 = (path.vectorPath[currentWaypoint] - transform.position).normalized
         dir *= speed * Time.fixedDeltaTime
         SimpleMove(dir)
         
         if Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance:
             currentWaypoint++
             return
             
     def SimpleMove(dir as Vector3):
         #rigidbody.MovePosition(rigidbody.position + dir)
         transform.Translate(dir)
 
               Mouse event function:
     def Update():
         if Input.GetMouseButtonDown(0):
             ray as Ray = camera.main.ScreenPointToRay(Input.mousePosition)
             hit as RaycastHit
             if Physics.Raycast(ray, hit, 100):
                 if not hit.transform.CompareTag("Background"):
                     Debug.Log("We're selecting: " + hit.transform.name)
                     selectedUnit = hit.transform.GetComponent[of Unit]()
                 else:
                     selectedUnit = null
 
              Try just using a normal update function for the manager script (the one the raycast is in).
Thanks for the suggestion. Unfortunately, I've already tried that. It doesn't seem to matter what update function I put the Raycast in; whether Update(), FixedUpdate(), or LateUpdate(), I still can't detect the collider while it's moving. 
When you don't use physics or have a higher order movement calculation (something that involves t² or t³) there's no reason why you should put it in FixedUpdate. Never use FixedUpdate to process one-time input events like key down / mouse button down. FixedUpdate don't run every frame if you have a high frame rate so it might miss the frames where the down / up event happened.
It would help to see the relevent parts of your script.
The reason the movement calculation is in the FixedUpdate function is because colliders' positions are only updated every FixedUpdate. It was an attempt to make sure the mouse events were occurring at a time when the position of the collider was where it really was. Question edited with relevant code. 
Your answer