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…