Raycasting detection

So since I’m making a RTS, I was wondering, how could I get it to make it so that it tells that a var is that object?

	if(Input.GetMouseButtonDown(0))
	{
		var path = Camera.main.ScreenPointToRay(Input.mousePosition);
		var layerMask = 1 << 8;
		if(Physics.Raycast(path,900000000,layerMask))
		{
			selected=path;
			path.selfSelected=true;
		}
	}

Could you try re-stating your question in a little more detail? I’m not sure what ‘it’ you want which ‘it’ to tell what to…

Basically, I’m wanting this var to be a reference or what you’d prefer to call it to be a game object, and so I was wondering how I do that with the object that was collided with.

The Physics.Raycast function can optionally take a parameter of type RaycastHit. This contains information about the object that was hit by the ray:-

var hit: RaycastHit;

if(Physics.Raycast(path, hit, 900000000,layerMask)) 
{ 
   var hitObject = hit.transform;
     ...
}

Er… I need to access the parent, so how do I do that from the access provided by the raycast? :stuck_out_tongue:

From my RTS (single click on somewhere, no dragging):

public void Mouse1Up (Vector2 screenPosition)
	{
		
		mouseButton1UpPoint = screenPosition;
		RaycastHit hit;
		
		//print("currently selected units: " + UnitManager.GetInstance().GetSelectedUnitsCount());	
		mouseLeftDrag = false;
		
		if (IsInRange (mouseButton1DownPoint, mouseButton1UpPoint)) {
			// user just did a click, no dragging. mouse 1 down and up pos are equal.
			// if units are selected, move them. If not, select that unit.
			if (UnitManager.GetInstance ().GetSelectedUnitsCount () == 0) {
				// no units selected, select the one we clicked - if any.
				Ray ray = Camera.main.ScreenPointToRay (mouseButton1DownPoint);
				if (Physics.Raycast (ray, out hit, raycastLength, nonTerrainLayerMask)) {
					// Ray hit something. Try to select that hit target. 
					//print ("Hit something: " + hit.collider.name);
					//hit.collider.transform.root.SendMessage ("SetSelected");
					Unit unit = hit.collider.transform.root.gameObject.GetComponent<Unit> ();
					if(unit != null)
						UnitManager.GetInstance().AddSelectedUnit(unit);
				}
				
			}
			/*else {
			// untis are selected, move them. Unit Manager's unit count is > 0!
			UnitManager.GetInstance().MoveSelectedUnitsToPoint(mouseButton1DownTerrainHitPoint);

For the Unit class, you can view it as a GameObject, because I use Unit just to have a bit better control :slight_smile:

It will work even if you click on the turret and not the Unit itself (Unit is tied to the first GameObject, root)
PS: the code extends on the RTS project you can find somewhere in this forum

Alright after testing a bit, I think I can tell that it detects when something is hit, however I believe the problem is it isn’t returning the object, any ideas?

Physics.Raycast doesn’t return the object, it just returns true or false to indicate if it hit anything or not. However, if you pass in a RaycastHit, it will be set when the Physics.Raycast returns true (and will not be set when Physics.Raycast returns false).

If you have a situation where Physics.Raycast is returning true, but your RaycastHit is not being set, there’s definately a problem. More likeyly, it’s being set but not to what you expected. In that case, check that the layer mask you pass into Physics.Raycast is correct.

It’s correct, however it’s still not working. :frowning: Please someone help.

Help how? If it’s correct, you don’t need help. If it’s not working, we need to know how it’s not working. Post more information.