Hi friends !
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
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?
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?
Thank you very much Unity community!