Instantiate an object on top of another?

Hi, first of all I am new to unity and new to javascript and so far am kind of getting the idea. But I have run into a slight problem that I cant work out. I am trying a side scroller platform game as my first project. I have a block that is in the air sort of like a mario "?" block. I have written a script that will detect if the player is hitting it from below. What I cant work out is how to Instantiate a new object, say a collectable to appear on top of the block when it is hit.

Is there any way to get the current blocks position and then increase the y value so it appears on top of the block?

Can we see the script that detects player collision?

2 Answers

2

use Instantiate (CoinPrefab,transform.position,transform.rotation) to get the coins in the right place then add a script to the coin prefab that uses transform.translate along the y axis and even transform.rotate to make it spin. Then have it destroy (gameObject,1) after 1 second (or what ever amount of time you want, it's the number after gameObject,)

Put this script on your block that will emit the coin and set item to the coin prefab. Adjust the offset if your block is larger than 1 unit.

var item : GameObject;
var offset : Vector3 = Vector3.up;
var emitOnce : boolean = true;

function OnHatted() {
    if (!enabled) return;
    Instantiate(item, transform.position + offset, Quaternion.identity);
    if (emitOnce) enabled = false;
}

Change your script that knows if the block was "Hatted"

Hatted = funny conceptual name for jumping onto something hat first :slight_smile:

// ... figure out if the target was Hatted (hit from below)

if (targetWasHatted)
     target.SendMessage("OnHatted", SendMessageOptions.DontRequireReceiver);

// ...

Your coin prefab can have an animation that plays on start, so it "jumps out" of the block automatically.

You coin prefab could also have an function OnTouch() that you call for any triggers you walk by to give the player some score and delete the coin.