x


Parenting after X time with collision, I dont get it!

Hey,

I need help with parenting an Object to an other after a collision occurred for a specified amount of time generally spoken. I will try to explain it more detailed, but first I want to let you know that I´am trying this for a few days now and I don´t get it working properly.

We have Object A, this is a moving Prefab and we have Object B it has a collider and a script that should wait for collision with Object A, and if the collision is happening for the amount of X time Object B should become the parent of Object A. If A and B are colliding for a lower time than X no parenting should happen.

alt text

-Yeah, sounds simple but not for me I really don´t get it working, I have not much programming knowledge and I think I´am missing a basic programming concept. I wrote a lot of diffrent scripts the last days but none of them worked properly, and I know most of you do not respond to Questions that have no scripts attached but I want to understand(!) not guess what is right so please help.

Thanks in advance for the time you invest,

GameGuy

problem_visual.png (17.8 kB)
more ▼

asked Apr 09 '12 at 08:11 PM

GameGuy gravatar image

GameGuy
610 22 28 40

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

This is a simple example that should do what you need. You could use OnTriggerEnter and OnTriggerExit instead of a constantly running checks in OnCollisionStay but this is more informative than actual implementation. This pseudo-code is not performance-friendly, but perhaps it'll give you hints.

public float collisionRequired = 2.5f;
private float collisionCurrent = 0.0f;

function OnCollisionStay(Collider coll)
{
    collisionCurrent += Time.deltaTime;

    if(collisionCurrent > collisionRequired)
        if(coll.transform.parent != transform)
            coll.transform.parent = transform;
}
more ▼

answered Apr 09 '12 at 08:33 PM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

+1 and BIG THANKS BY0LOG1C, I turned your pseudo-code into working-code :) , and it does what i was not able to do! Please let me ask one more thing is it possible that code like that is not working with really small amounts of time?

Apr 09 '12 at 09:42 PM GameGuy

Well, I know physics is calculated by tick at interval. Would an object goes in and out the collider in-between ticks, it would go unnoticed. If you have a rigidbody, setting it to Continuous Dynamic collision detection would help, but that's a known (hardly fixable) issue.

Apr 09 '12 at 10:38 PM by0log1c

BTW, I hate to request thumbs-up but the norm on UA is typically to mark an answer as accepted once a question is solved.

Apr 09 '12 at 10:40 PM by0log1c

Makes sense if the physics step is actually higher than the required collision time. Thanks again for your help!

Apr 09 '12 at 11:48 PM GameGuy
(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:

x2584
x588
x142
x128

asked: Apr 09 '12 at 08:11 PM

Seen: 398 times

Last Updated: Apr 09 '12 at 11:49 PM