Moving Platform won't let me jump while on it

This is a Unity 2D game I’m making, with this script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingParts : MonoBehaviour
{
public Transform posA, posB;
public float speed;
Vector3 targetPos;

private void Start()
{
    targetPos = posB.position;
}

private void Update()
{
    if (Vector2.Distance(transform.position, posA.position) < 0.05f)
    {
        targetPos = posB.position;
    }

    if (Vector2.Distance(transform.position, posB.position) < 0.05f)
    {
        targetPos = posA.position;
    }

    transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.CompareTag("Player"))
    {
        collision.transform.parent = this.transform;
    }
}

private void OnTriggerExit2D(Collider2D collision)
{
    if (collision.CompareTag("Player"))
    {
        collision.transform.parent = null;
    }
}

}
For some reason, it won’t let me jump while I’m on the platform, which really limits what I can do, can someone tell me how to fix this?

Print out what the jumpForce value is. Don’t assume it is what you set in code above. Here’s why:

Serialized / public fields in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:

  • what the class constructor makes (either default(T) or else field initializers, eg “what’s in your code”)

  • what may be saved with the prefab

  • what may be saved with the prefab override(s)/variant(s)

  • what may be saved in the scene and not applied to the prefab

  • what may be changed in the scene and not yet saved to disk

  • what may be changed in OnEnable(), Awake(), Start(), or even later

Make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

Here’s the official discussion: Serialization in Unity

If you must initialize fields, then do so in the void [icode]Reset()[/icode] method, which ONLY runs in the UnityEditor.

Here’s more nitty-gritty on serialization:

Field initializers versus using Reset() function and Unity serialization:

To avoid complexity in your prefabs / scenes, I recommend NEVER using the [icode]FormerlySerializedAsAttribute[/icode]

@Kurt-Dekker bro im so dumb I did the wrong script, sorry about that, will edit it now.

@makaka-org so you’re saying I have to add something into Update() so that it lets me jump?

Please don’t make duplicate posts, stick to a single thread please.

Thanks.

1 Like

As @MelvMay indicated, I will answer in the mentioned duplicated topic.

1 Like