- Home /
Problem with Falling Platform Script
So I basically watched GucioDev's video for the tutorial about falling platform. Here is the video
but however, for some reason I'm not able to get it working. Here is the script that I used to test it out (same script just like in the video but without "GetComponent().isTrigger = True;" part)
The Script:
using UnityEngine; using System.Collections;
public class FallingPlatform: MonoBehaviour {
private Rigidbody2D rb2d;
public float FallDelay;
void Start ()
{
rb2d = GetComponent<Rigidbody2D>();
}
void OnCollisonEnter2D (Collision2D col)
{
if (col.collider.CompareTag("Player"))
{
StartCoroutine(Fall());
}
}
IEnumerator Fall()
{
yield return new WaitForSeconds(FallDelay);
rb2d.isKinematic = false;
yield return 0;
}
}
Hi SirAlfi , do u find any solution for that its not working for me tow .
I just realised you spelled "OnCollisionEnter2D" incorrectly. Fixing that will make your code work correctly
Answer by RSharma98 · Aug 26, 2016 at 11:11 AM
It should be: StartCoroutine("Fall");
Rather than: StartCoroutine(Fall());
Hope that helps
Read the documentation before affir$$anonymous$$g something :
https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html
@Hellium. You can also use the method I suggested, scroll down on the page and you will see. I have been using this method for a while and this works perfectly fine
Yes I know, but using the StartCoroutine with the string parameter :
Does not let you to keep a reference of your coroutine
Does not let you to refactor your coroutine
Does not let you to track where your function is called using your IDE
I don't know why Unity keeps this in the specifications
Oh ok. I never knew that. Thanks for letting me know
Answer by OncaLupe · Dec 09, 2015 at 07:44 PM
Code looks fine to me. Double check the player's tag is exactly "Player" with capital letter, gravity scale is set on the platform's rigidbody, and it's not set to freeze position under Constraints.
You can add some Debug.Log() to see exactly what is or is not running. For instance, put Debug.Log(col.collider.tag);
at the top of the OnCollisionEnter2D to see what (if any) is being collided with. Probably another Log inside the if() and in the Fall() method to make sure it's starting.
Your answer
Follow this Question
Related Questions
Problem with 2D movement. My character is moving by himself between two points... :) 1 Answer
2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers
How can I make an object stop all momentum and hold it's position in air? 1 Answer
Why didnt my Collider works? 0 Answers
Why is there a gap between my player and the wall during wall slide? SOLVED 2 Answers