- Home /
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
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?
You can see his debug.DrawRay and 0.1 is fine i have tested it and it works for me :)
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
Answer by Bunny83 · Apr 21, 2011 at 09:58 AM
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!");
}
}
THAN$$anonymous$$ YOU SO $$anonymous$$UCH :D it worked :) i dont think i would ever of figured that out :P Thanks a million
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
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 ins$$anonymous$$d 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;
Answer by MC HALO · Apr 21, 2011 at 03:18 AM
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:
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 :)
The definition he's used works just as well. Check http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
ive tried with the hit and with out it still doesn't work :\
Your answer
Follow this Question
Related Questions
Raycasting problems 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Raycast help..... 2 Answers
Turret that shots a raycast to detect my character then shots at him. 1 Answer