Hi. I understand this issue has been asked several times but it seems like I still can’t find a workaround to the problem.
As of now, I’m currently using the script that is provided in this video:
Using a character controller (first person), there seems to be no problem at first. It seemingly sticks to the platform but issues started arising.
For example if I’m moving from point A to B horizontally,
|Terrain|A-------B.
If I hop from the terrain onto A, it sticks to the platform as it moves towards B. It continues sticking as the platform travels back from B to A. But as it moves from A to B again, the character stops sticking and I’m left hanging in the air until I start moving and I fall.
Another problem is if I have a vertically moving platform. There isn’t any problem with the downwards traveling. But as it goes up and reaches its peak height, I just fall through the platform.
Could someone advise me on this? Thanks!
Edit: Sorry! Here’s the code. The author stated it’s free to share/use so I believe it shouldn’t be a problem. All credits go to him (Kyle D’Aoust)
using UnityEngine;
using System.Collections;
public class MovingPlatform_CSHARP : MonoBehaviour
{
public Transform DestinationSpot;
public Transform OriginSpot;
public float Speed;
public bool Switch = false;
void FixedUpdate()
{
// For these 2 if statements, it's checking the position of the platform.
// If it's at the destination spot, it sets Switch to true.
if(transform.position == DestinationSpot.position)
{
Switch = true;
}
if(transform.position == OriginSpot.position)
{
Switch = false;
}
// If Switch becomes true, it tells the platform to move to its Origin.
if(Switch)
{
transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed);
}
else
{
// If Switch is false, it tells the platform to move to the destination.
transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed);
}
}
}