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:
Whatever value you set and save in your scene or prefab will wipe out the field initializer value you change in the code above.
This is a common source of confusion: the object is created, so at that instant in time it has the value you set in code, but before the object is put into use, if Unity has a saved scene or saved prefab that this is already in, then the value saved is overwritten.
It’s best to remove the initialization value above and instead provide it in a Reset function:
public f…
If something is public:
The instant you add that to your scene / prefab it is 10f … AND THEN IT GETS SERIALIZED.
Now go change the value in code. Note that it has no effect: the serialized value writes over the code value.
This is how Unity serialization works.
If you want to define it in code, make the variable private.
If you want to define it in the editor, make it public and DO NOT INITIALIZE IT.
Instead, make a Reset function, which ONLY runs at the instant it is added:
void Reset(…
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.
You use parenting.
When OnTriggerEnter2D() happens, the Player is moved with conditions described in Update() of MovingParts class that dominates over any other movements like jump.
1 Like
As @MelvMay indicated, I will answer in the mentioned duplicated topic.
1 Like