- Home /
Platform falling script
I have made a script were when my ball hits the platform it falls at a certian amount of time but when the ball actally hits the platform the platform wont fall how can i fix it?
#pragma strict
var destroyTime : float;
function OnCollisionEnter(s : Collision)
{
if(s.gameObject.tag == "Sphere")
Destroy(gameObject ,destroyTime);
}
I am unsure about a few things in your question:
What do you mean by 'it falls at a certain amount of time'? Did you mean that you want the platform to fall then remove itself after a number of seconds?
How do you want the platform to fall? with Physics or with Animation? The code you have written tells the platform to remove itself from the game, ins$$anonymous$$d of falling.
Do either the platform or the sphere have Rigidbodies attached?
Answer by Dimling · Jul 18, 2013 at 03:16 PM
I guess the ball, representing the player, got a Rigidbody attached to it. Note that OnCollisionEnter is only called when a collider/rigidbody has begun touching another rigidbody/collider.
What I would do, would be to add the script to the ball (player). The script would look like this (in C#):
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float timeToDestroy = 2.0f;
void OnCollisionEnter(Collision c){
if(c.gameObject.name.Equals("Platform")){
Destroy(c.gameObject, timeToDestroy);
}
}
}
Then when you press play and the ball collides with the platform it will be removed after the time you want. In my case i just wrote it static (2.0f). Of course this is just removing/delting/destroying the object or the platform. If you want it to fall you need to rewrite the code a bit and add some movement, but this will give you a headstart at least. Also remember to change the name of the platform to Platform if the script should work! ;)
Good luck!
Sources: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html http://docs.unity3d.com/Documentation/ScriptReference/Collision.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613110341im_/https://answers.unity.com/themes/thub/images/avi.jpg)