Hello,
I’m making a falling platform, what i mean by that is when my player is on this platform, it slowly falls. It works perfectly but only when the player touches this platform, if I’m out of this platform, it’ll stop falling. Is there a way to avoid that ? Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallingGround : MonoBehaviour
{
public Transform fallingGroundDestination;
public float speed = 1.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision other)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, fallingGroundDestination.position, step);
}
}
So I would like my platform starting to fall when I touch it, and still falling even if I’m somewhere else
Looks like maybe you can set a bool to indicate if it’s been touch by the player (to start). You can do that in OnCollisionEnter. Then, check for that variable (the bool) in Update, and if it’s true, do the MoveTowards.
Honestly, I’m not sure how it’s falling while you’re on it now, beyond it’s first descent. 
OnCollisionEnter only happens once when you enter the collider. Therefore, there is no “continuation” (standing on it or leaving…)
1 Like
Thanks ! It’s working, here is my code if that can help others peoples :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallingGround : MonoBehaviour
{
public Transform fallingGroundDestination;
public float speed = 1.0f;
private bool stillFalling = true;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (stillFalling == false)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, fallingGroundDestination.position, step);
}
}
private void OnCollisionEnter(Collision other)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, fallingGroundDestination.position, step);
stillFalling = false;
}
}
Cool, you’re welcome. Glad it’s working 