x


Detecting collision's parent?

I'm trying to make a little game where a complex car shoots another complex car. There are many joints in this car(s). I want them to all disable upon a condition (when the force of the bullet hit is greater than xx) so the car will just fall apart. My problem is the hierarchy of the entire car is very complicated, so I can't just index every single joint.

It's hard to explain, but I want to do something like this (logic steps):

When the bullet hits any part of the car, a function is called from that child. This function tells the car (the absolute parent for the whole car) it was hit, as a whole, and then a function in the parents script disables all the joints inside that car. But how can I call a function from a child of an unknown generation (I don't know how much of a child it is, could be 1, 2, 3, 4th child model)?

Thanks, Peter

more ▼

asked Feb 11 '10 at 11:05 PM

Peter B-B gravatar image

Peter B-B
236 13 15 21

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

4 answers: sort voted first

If your car has one parent and this parent is a "root" in your hierarchy (it has no parent itself) you can do something like this:

Transform t = transform;
do {
  t = t.parent;
} while (t != null);

If the car parent is not == null just look for the parents name instead null.

more ▼

answered Feb 11 '10 at 11:52 PM

StephanK gravatar image

StephanK
6k 40 53 93

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

SendMessageUpwards() is what you need for this. have the child do something like this

gameObject.SendMessageUpwards ("ApplyDamage", 5);

Only the car object should have an ApplyDamage method/function so other children will ignore it and just pass the message up. Since this message is not passe that often it is OK to send messages around. If you will be passing many messages each frame you should cache a pointer to car in the childern and call the applyDamage directly.

cheers, Grant

more ▼

answered Feb 12 '10 at 01:11 AM

Ryuuguu gravatar image

Ryuuguu
454 13 18 29

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

spree's answer is correct, but a simpler way would be to just use the root variable of Transform, which returns the topmost transform, i.e the parent of your hierarchy

just like

var T : Transform = transform.root;

from there you can access its components etc. etc.

more ▼

answered Feb 12 '10 at 03:40 AM

Eduardo Meade gravatar image

Eduardo Meade
166 1 2 5

Wow, never heard of that member. Guess I should read the manual more often. ;)

Feb 12 '10 at 10:34 AM StephanK
(comments are locked)
10|3000 characters needed characters left

define the function that you want in a script and attach that script to your parent GameObject. the name of that function should be unique. you can call GameObject.SendMessageUpwards in any of childern to call that function in their parents. if they have multiple parents then they'll try to call that in all of them so you should attach the script just to the parent that you want to have the script. also you can get the transform.root to get the root of your gameObject and then use GetComponent to get a reference to the script that you want and call its functions. this method is better and faster. call transform.root and GetComponent once in start function of all children and store the result in a variable and then in all collision use that value. avoid using SendMessage functions as much as possible. the function is too slow. when you call it, it will search all your child/parent gameObjects and all of their scripts for the function that you specified.

more ▼

answered Feb 26 '10 at 05:05 AM

Ashkan_gc gravatar image

Ashkan_gc
9.3k 33 56 120

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

x142

asked: Feb 11 '10 at 11:05 PM

Seen: 1515 times

Last Updated: Feb 11 '10 at 11:05 PM