x


ray cast not working ?

im trying to cast a ray from my player that will detect object it touches a certain length in front of him. i swear my code is fine but i cant get it to work

var xDirection = 15;
var yDirection = 0;
var zDirection = 45; 
var length = 0.1;
var hit : RaycastHit;

function Update () {
var diagonal = transform.TransformDirection (Vector3(xDirection,yDirection,zDirection));
Debug.DrawRay(transform.position, diagonal * length,Color.green);


if (Physics.Raycast(transform.position, diagonal,length)) {
    Debug.Log ("There is something in front of the object!");
}

}

help would be great :)

EDIT ANOTHER PROBLEM.....

var Bullet1_direction = Vector3(15,0,45);
var Bullet2_direction = Vector3(0 ,0,1);
var Bullet3_direction = Vector3(-9 ,0,45); 
var length = 4;

function Update () {
var Bullet1_diagonal = transform.TransformDirection(Bullet1_direction);
Bullet1_diagonal.Normalize();

var Bullet2_diagonal = transform.TransformDirection(Bullet2_direction);
Bullet2_diagonal.Normalize();

var Bullet3_diagonal = transform.TransformDirection(Bullet3_direction);
Bullet3_diagonal.Normalize();



Debug.DrawRay(transform.position, Bullet1_diagonal * length,Color.green);
Debug.DrawRay(transform.position, Bullet2_diagonal * length,Color.red);
Debug.DrawRay(transform.position, Bullet3_diagonal * length,Color.blue);

if (Physics.Raycast(transform.position, Bullet1_diagonal,length)) {
    Debug.Log ("Bullet 1 hit green!");
}
if (Physics.Raycast(transform.position, Bullet2_diagonal,length)) {
    Debug.Log ("Bullet 2 hit red!");
}
if (Physics.Raycast(transform.position, Bullet3_diagonal,length)) {
    Debug.Log ("Bullet 3 hit blue!");
}

}

HELP PLEASE... @Bunny83

more ▼

asked Apr 21 '11 at 02:48 AM

Chris 35 gravatar image

Chris 35
86 14 14 21

Can you see the Debug.DrawRay in the Scene window? Is 0.1 long enough for the raycast length? Does the object that you expect it to hit have a Collider?

Apr 21 '11 at 03:48 AM Marowi

You can see his debug.DrawRay and 0.1 is fine i have tested it and it works for me :)

Apr 21 '11 at 04:04 AM MC HALO

yeah 0.1 is fine and ive tried 1,2,3 ect but still nothing, i only get a result if its like around 10000

Apr 21 '11 at 04:34 AM Chris 35
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I would say your problem is your direction. If you use Raycast with a length the direction is normalized! That means your ray is 0.1 units long. But if you draw your ray with diagonal * length the drawn ray will be 4.743 units long since your direction is 47,43... long. I don't even understand your direction. It's forward but a bit to the right? Try diagonal.Normalize(); or .normalized.

var direction = Vector3(15,0,45);
var length = 0.1;

function Update () {
    var diagonal = transform.TransformDirection(direction);
    diagonal.Normalize();
    Debug.DrawRay(transform.position, diagonal * length,Color.green);

    if (Physics.Raycast(transform.position, diagonal,length)) {
        Debug.Log ("There is something in front of the object!");
    }
}
more ▼

answered Apr 21 '11 at 09:58 AM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

THANK YOU SO MUCH :D it worked :) i dont think i would ever of figured that out :P Thanks a million

Apr 21 '11 at 11:06 AM Chris 35

could you please have a look at this bit of code, i cant get it to work im having the same problem with the numbers having to be decimal to get right distance rather then full numbers

Apr 22 '11 at 05:35 AM Chris 35

Sorry, i'm not at home ;) but i don't get what's your problem? If your problem is that you want to declare a float variable instead of an int, just add ".0" to your number. That will turn the variable into a float. var length = 4.0; You can even declare the type explicitly with: var length : float = 4.0;

Apr 23 '11 at 02:33 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

i think you forgot the hit :

var xDirection = 15;
var yDirection = 0;
var zDirection = 45; 
var length = 0.1;
var hit : RaycastHit;

function Update () {
var diagonal = transform.TransformDirection (Vector3(xDirection,yDirection,zDirection));
Debug.DrawRay(transform.position, diagonal * length,Color.green);


if (Physics.Raycast(transform.position, diagonal,hit,length)) {
    Debug.Log ("There is something in front of the object!");
}
}

hope it works

have you attached it to the right player? if you still can not get it to work send it to me to my e-mail and i can look at it for you. the project folder this is.

My e-mail:

Hotmail- gto_oni-eyes@hotmail.com

google:

Hummad.Nazir@gmail.com

i hope to hear from you :)

try my code:

 public  var RayCastHitDoor :float  = 5.0f;



// Update is called once per frame

function Update () {



var hit : RaycastHit ;



    Debug.DrawRay(transform.position, Vector3.forward * RayCastHitDoor, Color.red);

        if(Physics.Raycast(transform.position , Vector3.forward, hit, RayCastHitDoor))

    {

        Debug.DrawRay(transform.position,Vector3.forward * RayCastHitDoor , Color.green);

               if(hit.collider.gameObject.tag == "put your obj name in here "){


                   // then do something. in my case i have told the hit collider to play animation when it collides with the object that is tagged door

        hit.collider.gameObject.animation.Play("Door animation");   



        }



    }



}

instead of having animation you could have Destroy the object if you want to do that i would be like this:

     Destroy(hit..collider.gameObject);

and that should destroy the game object you can do what ever you like :)

more ▼

answered Apr 21 '11 at 03:18 AM

MC HALO gravatar image

MC HALO
878 74 94 111

Apr 21 '11 at 03:33 AM Marowi

yep true that works for me as well

Apr 21 '11 at 03:39 AM MC HALO

ive tried with the hit and with out it still doesn't work :\

Apr 21 '11 at 04:35 AM Chris 35

send it to me i will have a look

Apr 21 '11 at 05:40 AM MC HALO

here is my e-mail address :

gto_oni-eyes@hotmail.com

Apr 21 '11 at 05:41 AM MC HALO
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5077
x3460
x1529
x180
x172

asked: Apr 21 '11 at 02:48 AM

Seen: 3291 times

Last Updated: May 06 '11 at 10:39 AM