How to prevent raycast go through several objects

Hello,

In the scene I have a board( similar like a chess board)and more than 30 cubes. What I need to do is moving the cubes on the slots by clicking them. The problem is when I choose the cube and if there is another cube behind it, ray goes through that object and transform both of them. Is there any way to prevent the ray going through objects.

Thanks
[/img]

If you use Physics.Raycast you should only be getting the first collision.

What could potentially be happening is you’re somehow “disabling” the first cube when you start to drag it, and also you keep doing the Raycast, hence the Raycast hits the second cube.

Just an idea.

If this does not help you, please elaborate a bit more.

Can we see your full code?

@nvarcha- Thanks for the info. I think I got the problem what I was doing is using both physics.raycast and collider.raycast inside the same cube script and doing wrong calculations. Now I understand the problem, but for some reason when I change the collider.raycast into a physics.raycast it effects all my cubes.

Here is the cube script

private var Holding = false;
private var IsUp=false;
function Start()
{
	for(var i = 1; i < 37; i++)
	{
		if(Vector3.Distance (transform.position, GameObject.Find("GameObject"+i).transform.position) < 50)
		{
					transform.position=GameObject.Find("GameObject"+i).transform.position;
		}	
	}
}
function Update () 
{
	
		if (Input.GetMouseButtonDown(0)==true) 
		{
			var hit : RaycastHit;
    		var ray : Ray= Camera.main.ScreenPointToRay(Input.mousePosition);
    		if (Physics.Raycast (ray, hit,Mathf.Infinity)) 
   	 		{
				Holding=true;
				renderer.enabled = false;
	
    		}
		}
		
		if(Holding==true  Input.GetMouseButtonUp(0)==true)
    	{
   			IsUp=true;
   			
   		}
    	if(IsUp==true  Input.GetMouseButtonDown(0)==true)
    	{
    		renderer.enabled = true;
    		IsUp=false;
    		Holding=false;
    	
    	}
    	
    	
    	
    	if(Holding == true)
    	{	
    		
    		var rayy = Camera.main.ScreenPointToRay (Input.mousePosition);
			var hitt : RaycastHit;
			if (Physics.Raycast (rayy, hitt,Mathf.Infinity)) 
		{
		
			for(var i = 1; i < 37; i++)
			{
				if(Vector3.Distance (hitt.point, GameObject.Find("GameObject"+i).transform.position) < 50)
				{
						transform.position=GameObject.Find("GameObject"+i).transform.position;
				}	
			}
		}
		
		
   		}
    
}

I see you’re doing two Physics.Raycast in the same frame.

Is there a way to avoid it? That might be causing problems.

You could save the collider collided with the first Raycast and use it if the user is still “Holding” the cube.

Does this make sense?

@nvarcha- I posted the wrong script. I changed it after talked to you.

here is the original code;

private var Holding = false;
private var IsUp=false;
function Start()
{
	for(var i = 1; i < 37; i++)
	{
		if(Vector3.Distance (transform.position, GameObject.Find("GameObject"+i).transform.position) < 50)
		{
					transform.position=GameObject.Find("GameObject"+i).transform.position;
		}	
	}
}
function Update () 
{
	
		if (Input.GetMouseButtonDown(0)==true) 
		{
			var hit : RaycastHit;
    		var ray : Ray= Camera.main.ScreenPointToRay(Input.mousePosition);
    		if (collider.Raycast (ray, hit,Mathf.Infinity)) 
   	 		{
				Holding=true;
				renderer.enabled = false;
	
    		}
		}
		
		if(Holding==true  Input.GetMouseButtonUp(0)==true)
    	{
   			IsUp=true;
   			
   		}
    	if(IsUp==true  Input.GetMouseButtonDown(0)==true)
    	{
    		renderer.enabled = true;
    		IsUp=false;
    		Holding=false;
    	
    	}
    	
    	
   
    	if(Holding == true)
    	{	
    		
    		var rayy = Camera.main.ScreenPointToRay (Input.mousePosition);
			var hitt : RaycastHit;
			if (Physics.Raycast (rayy, hitt,Mathf.Infinity)) 
		{
		
			for(var i = 1; i < 37; i++)
			{
				if(Vector3.Distance (hitt.point, GameObject.Find("GameObject"+i).transform.position) < 50)
				{
						transform.position=GameObject.Find("GameObject"+i).transform.position;
				}	
			}
		}
		
		
   		}
    
}

I can’t use Phyics.raycast because it affects all the object, and at the same time I can’t use collider.raycast because this time it passes through other objects and affects both.

If you place that on all your cubes it, all your cubes will use it. You have a couple options:

Create some other object whose purpose is to act as the player, or place the raycasting on the camera (When there is only one camera).

The other option is inside the Raycast success do a second check: if(hit.collider.gameObject == gameObject)

You need to verify that the object you hit is the object that holds this script, and not just any object (as raycast returns true if it hit something, not just if it hit the object calling the raycast).

@Ntero: Thanks a lot you saved my day. Now its working perfectly.