Question Physics.Raycast collider size relationship

Hi friends :smile:!

I’ve just use Unity for 2 days and totally love it. I’m trying to use a raycast function to make interaction between player and object such as door or elevator. My script is as follow:

var rayCastLength = 5;

function Update()
{
var hit : RaycastHit;

//check collision
if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
{
//…with an object
if(hit.collider.gameObject.tag == “cube”)
{
//move object
hit.collider.gameObject.animation.Play(“cube”);
}
}
}

and my scene setup like this:

The result is no interaction, the box did not move up. But when I rotated the box like this:

There is interaction, the box moves up and down :face_with_spiral_eyes:

So I try with a cube shape: if the cube is small there is no collision detected but if the cube is big, the collision script can detect that. So do I have to use big size for raycast detection? and if I still want to use small or flat box, how can I make it work?:face_with_spiral_eyes:

By the way If I want to make the script run once ( when the player move near the object, it moves up only once not again when the player goes nearby for the second time), what script should I use? :face_with_spiral_eyes:

Thank you very much Unity community! :sunglasses:

check Cube’s BoxCollider, when it’s being edited, the green wireframe is the object that ray cast is detecting.

The ray must be going over the Cube because it’s on the ground, when it’s rotated, the ray hits the cube instead.

If you wanted the ray to still hit the Cube while it lies down, you can change BoxCollider’s size.

Click on cube at editor and change its BoxCollider object.

Thank u very much razodactyl ^^! I will try it ^^