Hi everyone,
This is the first time I write here, and I afraid it´s because I need some support ![]()
I have the classic moving platform, I have done this before several times without any issue, and that what´s driving me crazy haha:
Player jumps into a moving platform, by some basic circle “raycast” it checks the moving platform and the player object became a “child” of the platform. If the scale of the player is x=1, everythings goes well, but when the player if facing left, and the scale x=-1, if the player object left the platform que x position is multiplied by -1.
So if the transform x position is 16, it will became -16 after unpareting the player object from the platform. The scale will keep the correct value of -1.
So if the player left the platform it’s just teleported to another uncharted place of the scene.
Here’s the paltform code, nothing special, I tried some variants, even moving it using the rigidboy but similar result…
public class MovingPlatform : MonoBehaviour
{
public Transform target; //posición objetivo
public float speed; //velocidad de la plataforma
private Vector3 start, end;
private void Start()
{
if (target != null)
{
target.parent = null;
start = transform.position;
end = target.transform.position;
}
}
private void FixedUpdate()
{
if (target != null)
{
float fixedSpeed = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, fixedSpeed);
}
if (transform.position == target.transform.position)
{
target.transform.position = start;
}
if (transform.position == start)
{
target.transform.position = end;
}
}
private void OnDrawGizmos()
{
// Gizmos.DrawLine(transform.position, target.position);
}
}
Then, here are some of the , I suppose, importants lines for the player controller…
private void CheckSurroundings()
{
OTHER STUFF (isOnGround etc)
isOnMovingPlatform = Physics2D.OverlapCircle(transform.position - new Vector3(0, -0.1f, 0), groundCheckRadius, whatIsMovingPlatform);
}
(I have also tried using OnCollisionEnter2D, same result)
private void IsInMovingPlatform()
{
if (isOnMovingPlatform)
{
Collider2D temp = Physics2D.OverlapCircle(transform.position - new Vector3(0, -0.1f, 0), groundCheckRadius, whatIsMovingPlatform);
gameObject.transform.parent = temp.transform;
}
else
{
gameObject.transform.SetParent(null);
DontDestroyOnLoad(gameObject);
}
}
private void TurningPlayer()
{
facingRight = !facingRight;
transform.localScale = new Vector3(-1f * transform.localScale.x, 1f, 1f);
//Dust effect when turning
TurningEffect();
}
I just move the player with
myRB.velocity = new Vector2(mov.x * speed, myRB.velocity.y); // Regular player movement
being “mov.x” the classic getAXis…
I dont know what can be affecting the player transform X
Anything I may be missing?
Many thanks in advance.