Need to attach character to a moving platform

Here is my script for my moving platform. Can i add additional script to it so my player character moves with it?
I am new to writing script so if its a simple thing i apologize in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingPlatform : MonoBehaviour
{

public Transform platform;
public Transform startTransform;
public Transform endTransform;

public float platformSpeed;

Vector3 direction;
Transform destination;

void Start()
{
SetDestination(startTransform);
}

void FixedUpdate()
{
platform.GetComponent().MovePosition(platform.position + direction * platformSpeed * Time.fixedDeltaTime);

if (Vector3.Distance(platform.position, destination.position) < platformSpeed * Time.fixedDeltaTime)
{
SetDestination(destination == startTransform ? endTransform : startTransform);
}
}

private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireCube(startTransform.position, platform.localScale);

Gizmos.color = Color.red;
Gizmos.DrawWireCube(endTransform.position, platform.localScale);
}

void SetDestination(Transform dest)
{
destination = dest;
direction = (destination.position - platform.position).normalized;
}
}

Please use: Using code tags properly - Unity Engine - Unity Discussions
You can edit your post & use them in the future, too. :slight_smile:

Thanks, like I said I am new to this and coding :wink: here is the code. any help would be greatly received.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingPlatform : MonoBehaviour
{

    public Transform platform;
    public Transform startTransform;
    public Transform endTransform;

    public float platformSpeed;

    Vector3 direction;
    Transform destination;

    void Start()
    {
        SetDestination(startTransform);
    }


    void FixedUpdate()
    {
        platform.GetComponent<Rigidbody>().MovePosition(platform.position + direction * platformSpeed * Time.fixedDeltaTime);

        if (Vector3.Distance(platform.position, destination.position) < platformSpeed * Time.fixedDeltaTime)
        {
            SetDestination(destination == startTransform ? endTransform : startTransform);
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(startTransform.position, platform.localScale);

        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(endTransform.position, platform.localScale);
    }

    void SetDestination(Transform dest)
    {
        destination = dest;
        direction = (destination.position - platform.position).normalized;
    }
}

That’s looking much better. So, if your question is only “can you attach the player”, I don’t see why not. I’ve never done this myself, but try this and see how you like it: Unity - Scripting API: Transform.SetParent
You’ll notice there are 2 overloads to the function. If you don’t know what that means, there are 2 ways you can write the method; 1 has 1 argument (with a default second one chosen for you), and the second one let’s you modify the second argument.
I prefer to suggest/show people that option rather than just “transform.parent = someOtherTransform” which can work, but never gives you that second option.
If I missed something, let me know… If you try that, let us know how it goes :slight_smile:

Having a character move around on a moving platform is a recipe for glitchiness if physics is involved. One way to get around this problem is to create two ‘versions’ of your platform:

Platform1: the real platform that the player will see, and which will move;

Platform 2: A reconstruction of the real platform that does not move, which is hidden.

Then you can move the real character around on the hidden stationary platform with all kinds of physics, and copy its local transform information which you pass to a ‘puppet’ character (without rigidbody, animations or anything like that) which is what the player sees on the moving platform.

It’s a bit of a bother but doing it this way you can preserve complex physics movement without any trouble.

Cool, I never thought about that or even tried it… I see people asking this question every now and then. I like picking up some ideas/tips on the subject, in case I come across future posts :slight_smile:

1 Like

Picked it up myself from someone’s post on Unity Answers, can’t remember where though.

Nice :slight_smile: