So I have this script for a moving platform, when I’m on the moving platform, it doesn’t seem to let me jump. Anyone know what I should do?
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;
}
}
}
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.
Update:
When you use parenting, then the child (Player in your case) will follow the parent’s movements (moving platform in your case). Such way Unity works.
So is there any other method or script I could use to fix that, while still keeping the feature of the player following the moving platform when on it and stops when off of it?
So, the first one didn’t really work, but the second one I don’t really understand, if I make the box collder smaller, I’ll just glitch into the platform, with the same thing
Provide screenshots to understand what you are talking about
Oh and the script is actually called “MovingPlatform” I fixed that in script as well
You can also select all objects at once in Hierarchy View, and show all the colliders and triggers in Scene View, providing the screenshot or video demo.
Please understand, the more information you provide, the faster you get a solution. It is also useful for introspection and problem solving on your own.
Yeah, sorry about how long it’s taking for me to reply and low information, here you go
While I’m at it, the MovingPlatform, as shown in the screenshot, has all the scripts and colliders attached in its child, “Platform” shown in the third screenshot