- Home /
Simple and Quick Fix PickUp script
Guys, I am having an issue with a simple script. To make it quick, I have this script attached to my player. I have a couple spheres tagged coin. I collect them and it adds points to the score and destroys the sphere. That part is fine.
Recently I decided I did not want the sphere to destroy. I now wanted it to be spawned to a holding place for the spheres(Coins) in the level. So, I created an empty GameObject and used it as my spawn point. So now when I collect the coins, they all go to that location. Fine.
The problem is that I want them to collect inside the holding area realistically. Imagine a glass and you you drop marbles in it. So I set the spawn point above the container and added a rigid body to the spheres. The spheres now automatically fall through the plane. I tried making the colliders all primitive and tried making the plane a mesh collider and the spheres primitive but its a no go. They just fall through the floor. Where am I going wrong. And also is it possible that some one can give me hints as to the best way to approach using an array to keep track of the amount of spheres in the container? something like the container has 5 boxes then I can no longer collect any boxes.
var score = 0;
var scoreText = "Score: 0";
var track : AudioClip;
var packTruck : Transform;
function OnTriggerEnter( other : Collider ) {
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "Coin") {
Debug.Log("Other object is a coin");
score += 5;
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
audio.Play();
other.transform.position=packTruck.transform.position;
}
}
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), scoreText);
So I declare the trigger state to true initially and then once it is collected set it to false and add a rigidbody component within the script? But how would I add the component if the script is attached to the player and not the coin? also am I going about the moving the coin to the container, because the container will be attached to a moving object?
Would it be easier if I just destroy the object and spawn a prefab to the spawn location? If so how would I go about doing that.
Answer by deepthinker · Mar 07, 2012 at 12:15 PM
Put a box collider on the plane instead of a mesh collider.
Answer by aldonaletto · Mar 07, 2012 at 12:29 PM
Who's the trigger? The coins? If so, they will fall through any collider if a rigidbody is added. You can add a rigidbody only when placing the coin in the holder, and set the collider.isTrigger property to false to make it a regular collider.