x


Raycasting fail

Hello. I'm attempting to create a semi-complex door animation to activate when my characters raycast hits it, then it opens, and either:

Whilst the animation is happening it destroys the door's collider to prevent any possible glitches or replaying the animation when the raycast hits it

Or

Destory the door (it sinks into the floor, walls and roof) once the animation is over (so a timed function I guess)

Here's what I have so far

var rayCastLength = 10;

function Update()
{
    var hit : RaycastHit;

    //check if character is colliding with door
    if(Physics.Raycast(transform.position,transform.forward,hit,rayCastLength))
    {
       //with the character
       if(hit.collider.gameObject.tag == "door")
       {
         //open the door
         hit.collider.gameObject.animation.Play("Take 001");
         //destroy the door's collider
         Destroy(hit.collider.gameObject);
       }
    }
}

Thanks in advance for any and all replies :D

more ▼

asked Aug 20 '12 at 02:17 AM

LegibleMushroom gravatar image

LegibleMushroom
16 1 2

what exactly is the problem? are you asking how to make the animation or the code to trigger it? also I don't think you'd need to destroy the collider, you could simply do collider.enabled = false That will prevent it from interacting with other colliders.

is that what you want to do? (also i'd do the collider.enabled = false PRIOR to the play animation not after you already started so the begining doesn't wonk it up)

Aug 20 '12 at 06:20 AM sparkzbarca
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Alright, I eventually got it working.. 2 minutes ago.. Haha I didn't know about a collider.enabled >.< Here's my final code for anybody that has a similar problem

var rayCastLength = 10;

function Update()
{
    var hit : RaycastHit;

    //check if character is colliding with door
    if(Physics.Raycast(transform.position,transform.forward,hit,rayCastLength))
    {
       //with the character
       if(hit.collider.gameObject.tag == "door")
       {
         //open the door
         hit.collider.gameObject.animation.Play("Take 001");
         //destroy the door
         Destroy(hit.collider.gameObject,3);
       }
    }
}

What I had was an animation I created in 3DS Max along with the door itself, the animation was playing correctly and all, but all I needed was to be able to destroy either the door or the doors collider so I can walk through where the door was.

Thanks anyways :)

more ▼

answered Aug 20 '12 at 09:00 AM

LegibleMushroom gravatar image

LegibleMushroom
16 1 2

(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:

x3768
x3444
x1682
x1525
x192

asked: Aug 20 '12 at 02:17 AM

Seen: 355 times

Last Updated: Aug 20 '12 at 09:00 AM