- Home /
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.
][1]
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[i].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..
Answer by Fattie · Mar 22, 2013 at 07:15 AM
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
But my problem is that my bullet ignores in between sphere. And directly goes to the mouse position. what do i do for this problem ?
HOW BIG are your sphere colliders? it may be you have to make them all big enough that they re just about touching. It could be that simple.
$$anonymous$$ake screen shot OF YOUR EDITOR showing the rig ok?
Your answer
![](https://koobas.hobune.stream/wayback/20220613094851im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
First Person Controller Target Shooting With Gun 1 Answer
Object Does not Destroyed While Shooting 1 Answer
AI Shooting for Aircraft 1 Answer
A node in a childnode? 1 Answer
Why does the wrong enemy die with this raycast? C# 2 Answers