Using raycast as collision detection still causes objects to intersect

Hi all,

I’m currently working on a game that’s similar to Super hexagon, and requires controlling 2 cubes through a maze or randomly spawning objects.

I currently have a collision detection system by placing 4 gameObjects on the cube that fire rays in multiple directions. If the ray collides on the sides of the cubes, it stops the cubes movement.

Because of the speed of the cubes movement, the cubes sometimes stop their movement when they’re already in the obstacle, not before.

I’m sure this is happening as the cubes movement is being processed in the same update frame of the raycast check, meaning that the raycast stops the movement too late.

I currently have the lovely script “DONTGOTHROUGHTHINGS” attached to the objects, but this still isn’t solving my problem. Essentially, I want to make sure the cubes never intersect inside of another object.

Should I be storing the cubes previous location as a vector, then push it back to that location if its detected to be in another object? and if so, how would you go about doing this?

Many thanks for any help that could be provided.

Your proposed solution is good, you could just store last frame’s position and restore it like so:

Vector3 lastPos =;

void Update() {
  if( raytraceHit ) {
    ... do other stuff ...
    transform.position = lastPos;
  lastPos = transform.position;  

If you do not worry about performance too much (you don’t have lots of physics geometry) you could also consider changing the accuracy of physics simulation:

Go to Edit → Project Settings → Time and change Fixed Timestep to smaller value (like 0.01)