- Home /
what's wrong with my raycast object report code?
I'm working on a turn based grid game. think checkers. I'm really proud of myself for making the randomized grid map without any help from plugins, but the trouble started when I tried to make the objects start interacting. What I need this code to do is create a "go right" button which sends a ray from the object to the right one unit. if the raycast hits an object tagged enemy, I need the player object to move two units like he's jumping him (checkers). otherwise I just need the player object to move one space.
As you can see, I am having trouble just getting the ray to report an object. I have tried many solutions: from triggerEnters to Rays; from kinematic onTriggers to box collider rigidbodies. The only thing that seems to be consistent is that either collisions are ALWAYS happening or NEVER happening.
function OnGUI(){
if(myTurn){
//move/jump right
if(GUI.Button(Rect(Screen.width/2 + 100, Screen.height/2, 75,75), moveRightButton)){
var hit : RaycastHit;
var myRay = new Ray(myPos, Vector3.right);
if(collider.Raycast(myRay, hit,1)){
if(hit.collider.gameObject.tag == "Enemy"){
Debug.Log("this is awkward");
}
}
}
}
}
Mostly, I just need a fresh pair of eyes to make sure my ray is in the right format. If you can think of a better way to do this, let me know. I also want to make the player object keep "jumping" if more than one enemy is lined up. If you're feeling abstract, do you have any ideas for a looping function that repeats the jump routine until there's no more enemies? But most importantly, what's going on with my Raycast? Thank you very much for your time.
Answer by robertbu · Jan 21, 2014 at 03:05 AM
One issue is that you are using Collider.Raycast(). Collider.Raycast() only casts against a specific collider. In this case you are Raycasting against the object this script is attached to. Since you are inside the object and mesh are one sided, you won't hit anything. Replace your 'collider.Raycast' with Physics.Raycast:
if (Physics.Raycast(myRay, hit, 1)) {
Thank you! I will give it a try. I started off using Physics.Raycast, but couldn't get the layermask to work. Checking the hits for a tag wasn't producing good results either, but I'm going to try to move the objects off of the grid tiles in the z - axis, so there's nothing else for the ray to hit. Will report back on that. Can I tap you for ideas on how to make a function like... press button, raycast this direction, move this direction D units, where D is the amount of enemy raycastHits + 1.
Thanks again!
Layer masks are tricky since they are a bit set and not an integer. You have to set the right bit in the mask for it to work correctly. You don't include the code where you initialize your mask, so I cannot check it. Here is a bit of code that will jump over the thing that is found by the ray cast. It uses world directions not directions relative to the "character." I'm not sure of the design of your game:
function Update() {
var hit : RaycastHit;
var pos : Vector3;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
if (Physics.Raycast(transform.position, Vector3.right, hit)) {
pos = hit.transform.position;
pos.x += 1.0;
transform.position = pos;
}
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
if (Physics.Raycast(transform.position, Vector3.left, hit)) {
pos = hit.transform.position;
pos.x -= 1.0;
transform.position = pos;
}
}
}
Thank you very much for your help! I needed to write it out before I found a solution and you really cleared up rays for me. I can't copy the solution here because it's massive, but basically I just raycast in the direction and move and then do it again, up to five times. The only thing left to do now is copy that to 8 different directions. Woo! Thanks again