Hey folks,
since I don’t feel comfortable using rigidbodies, I’ve decided to write my own collision script using raycasts. The function below is called from the player script, which sends the corresponding data as well. ‘distance’ is the playerSpeed * Time.deltaTime
void CheckCollision(Vector3 rayPosition, Vector3 rayDirection, float distance)
{
RaycastHit rayHit;
if (Physics.Raycast(rayPosition, rayDirection, out rayHit, Mathf.Infinity))
{
float distanceWeCanMove = Vector3.Distance(position, rayHit.point) - minimumDistance;
if (distanceWeCanMove <= 0)
return 0f;
if (distanceWeCanMove >= distance)
return distance;
return distanceWeCanMove;
}
return distance;
}
At first the raycast distance was 1f. I choose this since distance is usually a small number, and it seemed efficient to not make the raycast too long. After doing lots of monkey-testing, it turned out the object could sometimes still glitch through walls. After changing the raycast distance to Mathf.Infinity however, this hasn’t happened anymore. How come the raycast distance makes such a big difference, while the the distance parameter is such a small value?