x


Scripting help :D

Hey guys and gals!

In my game, my character has no legs at the beginning but later on has the chance to gain them back and become fully standing. For this I created two separate models of the character, one with legs and one without in hopes than I can swap between them.

The legless version would be replaced by the standing version permanently but I can't seem to get the script right. Here's what I have below;

var legReplacementBolt: GameObject[]; 

function OnCollisionEnter (collision : Collision)
    // Destroy enemy
    Destroy(gameObject);

    // Replace enemy with the dead body

    if(collision.gameObject.tag == "Player") {
        var legReplacementBolt: Transform = Instantiate(legreplacementbolt, transform.position, transform.rotation);
    // Copy position & rotation from the old hierarchy into the dead replacement
        CopyTransformsRecurse(transform, dead);
        return;
    }

static function CopyTransformsRecurse (src : Transform, dst : Transform) {

    dst.position = src.position;
    dst.rotation = src.rotation;

    for (var child : Transform in dst) {
        // Match the transform with the same name
        var curSrc = src.Find(child.name);
        if (curSrc)
            CopyTransformsRecurse(curSrc, child);
        }
    }

Any help would be much appreciated :D Liban Ali

more ▼

asked May 15 '12 at 02:20 AM

Liban gravatar image

Liban
0 1 2 2

Is this your entire script? I just tried to fix the formatting, but it turns out that the script doesn't actually make any sense! Make sure you have the right number of open and close braces.

It looks like you've adapted this from a different script (probably one that automatically converts objects into other objects), but without really understanding why you have to do that. In your case, you should probably not use a script that recursively matches all the bones in a skeleton, because presumably you are using an animation on both (which will instantly wipe that match in the next frame).

May 15 '12 at 02:28 AM syclamoth

Is this your entire script? I just tried to fix the formatting, but it turns out that the script doesn't actually make any sense!

I agree with this I've read the script a few times I cannot understand it either. I don't like your variable naming either. What exactly is going wrong with the script, maybe if I knew what was wrong I'd knew where to look for the problem.

May 15 '12 at 03:02 AM KingCharizard

Ahh sorry about that! Here it is below; var BoltReplacement: Transform;

function OnTriggerEnter(hit : Collider) { // enemy hit by your sword if(hit.collider.tag == "playerswitch") { Destroy(gameObject); } if (BoltReplacement) { var dead : Transform = Instantiate(BoltReplacement, transform.position, transform.rotation); CopyTransformsRecurse(transform, dead); } if(!hit.collider.tag == "playerswitch") { return; } } static function CopyTransformsRecurse (src : Transform, dst : Transform) { dst.position = src.position; dst.rotation = src.rotation; }

I got it semi working however the big problem here is that when touching a collider seperate to the one tagged "playerswitch" the instantiate function is processed however the character model shows up 3 - 4 more times.,

May 15 '12 at 04:18 AM Liban

@Liban. In comments, put your code inside pre and code brackets like this:

 <pre><code>

... code here ...

</code></pre>

May 15 '12 at 08:28 AM bompi88

@Liban

""I got it semi working however the big problem here is that when touching a collider seperate to the one tagged "playerswitch" the instantiate function is processed however the character model shows up 3 - 4 more times.,""

This is because as long as your still in the collsision area the instantiate is being called. Try to make a variable that allows u to stop instantiate with a do while loop... so it does once but after the condition such as playerSwitched = true. Then stop the loop this way it will only run once and if the playerSwitched variable returns true it will break out the loop.

May 15 '12 at 03:15 PM KingCharizard
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
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:

x5049
x3442
x1937
x136
x51

asked: May 15 '12 at 02:20 AM

Seen: 395 times

Last Updated: May 15 '12 at 03:17 PM