Getting 222 Errors, all "Scene::raycastClosestShape: The maximum distance must be greater than zero!"

Hello,

So I’m messing around with raycasting and making my player detect collisions on the right and left sides (it already detects above and below), and I’ve just gotten 222 errors, all saying “Scene::raycastClosestShape: The maximum distance must be greater than zero!
UnityEngine.Physics:Raycast(Ray, RaycastHit&, Single, Int32)
PlayerPhysics:Move(Vector2) (at Assets/Game Game Game/Scripts/PlayerPhysics.cs:73)
PlayerController:Update() (at Assets/Game Game Game/Scripts/PlayerController.cs:39)”. Here is the script for the PlayerPhysics:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(BoxCollider))]
public class PlayerPhysics : MonoBehaviour {

public LayerMask collisionMask;

private BoxCollider collider;
private Vector3 s;
private Vector3 c;

private float skin = .005f;

[HideInInspector]
public bool grounded;

Ray ray;
RaycastHit hit;

void Start() {
	collider = GetComponent<BoxCollider>();
	s = collider.size;
	c = collider.center;
}

public void Move(Vector2 moveAmount) {
	
	float deltaY = moveAmount.y;
	float deltaX = moveAmount.x;
	Vector2 p = transform.position;
	
	// Check collisions above and below
	grounded = false;
	
	for (int i = 0; i<3; i ++) {
		float dir = Mathf.Sign(deltaY);
		float x = (p.x + c.x - s.x/2) + s.x/2 * i; // Left, centre and then rightmost point of collider
		float y = p.y + c.y + s.y/2 * dir; // Bottom of collider
		
		ray = new Ray(new Vector2(x,y), new Vector2(0,dir));
		Debug.DrawRay(ray.origin,ray.direction);
		if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaY) * skin,collisionMask)) {
			// Get Distance between player and ground
			float dst = Vector3.Distance (ray.origin, hit.point);
			
			// Stop player's downwards movement after coming within skin width of a collider
			if (dst > skin) {
				deltaY = dst * dir - skin * dir;
			}
			else {
				deltaY = 0;
			}
			
			grounded = true;
			
			break;
			
		}
	}

	// Check collisions left and right
	
	for (int i = 0; i<3; i ++) {
		float dir = Mathf.Sign(deltaX);
		float x = p.x + c.x + s.x/2 * dir;
		float y = p.y + c.y - s.y/2 + s.y/2 * i;
		
		ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
		Debug.DrawRay(ray.origin,ray.direction);
		if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX) + skin,collisionMask)) {
			// Get Distance between player and ground
			float dst = Vector3.Distance (ray.origin, hit.point);
			
			// Stop player's downwards movement after coming within skin width of a collider
			if (dst > skin) {
				deltaX = dst * dir - skin * dir;
			}
			else {
				deltaX = 0;
			}

			
			break;
			
		}
	}
	

	
	Vector2 finalTransform = new Vector2(deltaX,deltaY);
	
	transform.Translate(finalTransform);
}

}

For reference, line 73 is “if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX) + skin,collisionMask)) {”. Thanks!

You calling Move() with moveAmount:

public void Move(Vector2 moveAmount)

The problem is that if either moveAmount.x or moveAmount.y is 0.0, then your calculated distance in your Raycast is 0. You shouldn’t pass 0 for the distance in a Raycast. And there is no reason to cast a ray if the move distance is zero anyway. You can do something like:

bool moveVert = !Mathf.Approximately(deltaY, 0.0f);
if (moveVert && Physics.Raycast(ray,out hit,Mathf.Abs(deltaY) * skin,collisionMask)) {

Same for horizontal:

bool moveHorz = !Mathf.Approximately(deltaX, 0.0f);
if (moveHorz && Physics.Raycast(ray,out hit,Mathf.Abs(deltaX) + skin,collisionMask)) {