2D : RayCast Problem

I have grid structure and i am arranging sphere in closest empty spaces like bubble shooter.

Now for shooting , i am using raycast concept.

[9189-8985-hexgridgame2.jpg*|9189]

In grid i have problem is that , I do not want to allow my sphre to cross the grid cell filled with blue color.

But bcz i have used raycast which takes mouse position. And in my case it ignores the in between sphere and collides with the sphere available at mouse position and takes the nearest empty position. But i do not want to allow them to cross. Same ting applies to newly assigned sphere, if they are in between the way then they also should not be crossed. So what do i need to do??

My Shooting Script:

function Update ()
{
	    emptyObject = GameObject.FindGameObjectsWithTag("Empty");
		for(var i : int = 0 ; i < emptyObject.Length ; i++)
		{
			emptyList.Add(emptyObject*.transform);*
  •  }* 
    
  •  Debug.Log("Length empty: " + emptyObject.Length);*
    
  •  if(transform.parent == null)*
    
  •  {*
    
  •  	  var hit : RaycastHit ;*
    
  •        var ray : Ray  = Camera .main .ScreenPointToRay (Input.mousePosition );*
    
  •        if(Physics.Raycast (ray, hit)){* 
    
  •  	     var tempx = hit.transform.position.x;*
    
  •  	     var tempy  = hit.transform.position.y;*
    
  •  	     target = Vector3(tempx,tempy,0);*
    
  •        }*
    
  •          if(target != null){*
    

_ var moveAmount : float = 5000 * Time.deltaTime;_
_ transform.rigidbody.AddForce(transform.TransformDirection(target) * moveAmount); _

  •       }* 
    
  •   }*
    

}
On collision :
function OnCollisionEnter(collision : Collision )
*{ *
if(transform.parent == null)

  •  {	* 
    
  •  var contact : ContactPoint = collision.contacts[0];*
    
  •  				var pos : Vector3 = contact.point;	*
    
  •  				var t : Transform = ClosestTransform(pos);*
    
  •  				transform.position = t.position;*
    
  •  				transform.rotation = t.rotation;*
    
  •  				transform.rigidbody.velocity = Vector3.zero;*
    
  •  				transform.rigidbody.angularVelocity = Vector3.zero;* 
    
  •  				transform.rigidbody.isKinematic = true;*
    
  •  				transform.parent = go.gameObject.transform;*
    
  •  				t.gameObject.tag = "";*
    
  •  }			*
    

}
Function :
function ClosestTransform(curr : Vector3) : Transform
{
var fDist : float = Mathf.Infinity;
var v3Curr : Vector3 = curr;
var trOut : Transform = emptyList[0];

  • for (var trans : Transform in emptyList) {*
  •     var fT : float= (trans.position - v3Curr).sqrMagnitude;*
    
  •     if (fT < fDist) {*
    
  •       trOut = trans;*
    
  •       fDist = fT;*
    
  •     }*
    
  •  }*
    
  • return trOut;*
    }
    Please help me. Thanks for your help and support…

Ekta, just so you know.

Many programmers would do a game like this, not using physics at all.

Imagine your hex number 371.

Six hexs are touching it. In fact it might be 370, 372, 339, 340, 401, 402

Note that normally you can figure-out the six, using a fairly simple formula.

Or, you can do this: for each hex (say 370), just keep the six neighbors in variables.

So, make a little structure with fields like “up and to the left” “right hand neighbor” etc. Compute all that at development time, or even just when the game launches.

It is then extremely easy for you to know neighbors during play.

While it is definitely possible (and possibly sensible) to use raycasting for such a game, it may be better to just do it logically, with some arrays.

Hoep it helps