Raycast All

Hi guys this is my first ever question, so go easy on me… I am casting a ray from the mouse to the terrain, I want check if the ray hits within a certain area if it does it will instantiate the road object at a predetermined grid location. however if there is already a road at that location then I want it to do nothing. Is this Possible??? Sorry its really sloppy at this point.

The explanation of raycast all is fairly weak. (In my opinion!)

Here is some of the code:

function BuildRoad()
	if (Buttons.toggleRoad == true)
            if (Input.GetMouseButtonDown(0))
   				if (GUIUtility.hotControl == 0)
    				var hits : RaycastHit[];
   					hits = Physics.RaycastAll (camera.main.ScreenPointToRay(Input.mousePosition),Mathf.Infinity);

    				for (var i=0;i<hits.length;i++)
        			var hit : RaycastHit = hits*;*
  •  	if(hit.collider.gameObject.CompareTag ("road"))*
  •  	{*
  •  	else if(hit.collider.gameObject.CompareTag("constructer"))*
  •  	{*
  •  	 		var ray = Camera.main.ScreenPointToRay(Input.mousePosition);*
  •  				var ent = 100.0;*
  •  			if (plane.Raycast(ray, ent))*
  •  			{*
  •  			var hitPoint = ray.GetPoint(ent);* 
  •  			var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("waypoints");* 
  •  					var closest: GameObject;*
  •  					var closestDist = Mathf.Infinity;*
  •  			for (waypoint in waypoints)* 
  •  					{* 
  •  					var dist = (hitPoint - waypoint.transform.position).sqrMagnitude;*
  •  					if (dist < closestDist)* 
  •  				{* 
  •  		closestDist = dist;* 
  •  		closest = waypoint;* 
  •  		}* 
  •  			}	*
  •  		var roadBlankPrefab = Instantiate (roadBlankPrefab,closest.transform.position, Quaternion.Euler(0, 0, 0));*
  •  		}*
  •  		}*
  •  	}*
  •  	}      		*
  •  }*
  • }*

Sorry but your code is a little full of IF statements :smiley:
I rewrote your code for analysis in footer of this answer…

Your question was “if there is already a road at that location then I want it to do nothing. Is this Possible?”

The answer is: the code already do nothing when hits a road… If you look at your code, you send this statement: if(hit.collider.gameObject.CompareTag (“road”)){}

So, you do nothing :smiley:

If you need a better explanation of RaycastAll, there it is: Physics.Raycast cast a ray from one point to another, and it stops when encounter a collider… RaycastAll doesn’t stop, and continue cast until the distance is reached (in your code infinity, so it isn’t a great solution)

void BuildRoad(){
	if (!Buttons.toggleRoad || !Input.GetMouseButtonDown(0) || GUIUtility.hotControl != 0)
	Ray mouseToScreenPos = camera.main.ScreenPointToRay(Input.mousePosition);
	RaycastHit[] hits Physics.RaycastAll(mouseToScreenPos, Mathf.Infinity);

	foreach(RaycastHit h in hits) {
		if(h.collider.gameObject.CompareTag("constructer")) {
			float ent = 100f;
			if(!plane.Raycast(mouseToScreenPos, ent))
			Vector3 hitPoint = ray.GetPoint(ent);
			GameObject[] waypoints = GameObject.FindGameObjectsWithTag("waypoints");
			GameObject closest;
			float closesDist = Mathf.Infinity;

			for(waypoint in waypoints){
				float dist = Vector3.Distance(waypoint.transform.position, hitPoint);
				if(dist < closestDist){
					closestDist = dist;
					closest = waypoint.transform.position;
			GameObject roadGO = Instantiate(roadBlankPrefab, closest, Quaternion.Euler(0, 0, 0));

I hope I’ve solved your question :smiley:

Sorry for the delay in response… turns out I didn’t need to use Raycast All went to a standard physics raycast and organized the hit levels of the objects does the trick for now. I will post an update of my code asap.