How to get a Character to move with a moving platform ?

I've been trying to get my character to move along with my moving platform (like when i jump unto the platform it carries me long with it), but so far have been unable to do so.

I have a character with CharacterController, it has also been tagged by ‘Player’.

My moving platform has a ‘Rigidbody’ attached to it, a 'Configurable Joint’and a ‘Box Collider’. The platform is tagged with ‘JumpPad5’.

This is the script attached to my moving platform:

var targetA : GameObject;

var targetB : GameObject;

var speed : float = 0.1;

function FixedUpdate ()

{

var weight = Mathf.Cos(Time.time * speed * 2 * Mathf.PI) * 0.5 + 0.5;
transform.position = targetA.transform.position * weight
                    + targetB.transform.position * (1-weight);

}

So if anyone can help me, pleaso do.

Can’t you make the player a child to the platform with On trigger stay/exit events? That way the player will move, rotate, etc with the platforms movements AND speed, while still having their own controls.

 function OnTriggerStay(other:Collider){
    		
    		if(other.gameObject.tag == "platform"){
    		transform.parent = other.transform;

		}
	}

function OnTriggerExit(other:Collider){
	if(other.gameObject.tag == "platform"){
			transform.parent = null;
			
		}
	}

i have a simple way.

1.add box collider to your platform and make sure that isTrigger is enabled

2.create new c# script

3.attach the c# script to the platform you want

4.copy script below to your c# script that you made

private GameObject target=null;
private Vector3 offset;

void Start(){
	target = null;
}

void OnTriggerStay2D(Collider2D col){
	target = col.gameObject;
	offset = target.transform.position - transform.position;
}
void OnTriggerExit2D(Collider2D col){
	target = null;
}

void LateUpdate(){
	if (target != null) {
		target.transform.position = transform.position+offset;
	}

}

good Luck

first share is here http://answers.unity3d.com/answers/1289185/view.html

I’ve been working on this for 2 days and finally found a way so that the character can stay on the platform and then be able to move around and continue to stay on the platform.

The principle is simple you want it so that when you collide with the moving platform that the character changes to kinematic mode and the parent is the collider that way the character moves with the collider.
so something like this:

void OnCollisionEnter2D (Collision2D coll)
{

	if (coll.transform.tag == "Moving Platform") 
	{
		GetComponent<Rigidbody2D>().isKinematic=true;
		transform.parent = coll.transform;
	}
}

Now that’s all well and good but now while it’s kinematic and parented it’s unable to move on it’s own and programming extra controls to control it in this state is a pain so what you want is that if you move or jump then the character is no longer kinematic or parented. This is best done in the functions update and fixedupdate because it will be checked every frame. So something like this:

	if (move != 0 && transform.parent != null) 
	{
		GetComponent<Rigidbody2D>().isKinematic= false;
		transform.parent = null;
	}

This command basically says if your moving and your parented then the player is no longer kinematic and is no longer parented. Then add in your command for jumping so something similar to this:

            if ((grounded) && Input.GetKeyDown (KeyCode.Space)) 
	{
		GetComponent<Rigidbody2D>().isKinematic = false;
		transform.parent = null;
            }

This way when you are grounded (on the moving platform) and when you press your jump key or function then the object is no longer kinematic and is no longer parented.

This way while you are in collision with the moving platform you will be kinematic and moving with the platform because it’s your object’s parent. But if you are moving or jumping then you are not moving with the platform and the platform is no longer your parent.

This is the first result I had when searching for the same thing, so if anyone sees this in 2022 or later you can try making a new Physics Material 2D with a friction of 10(approx.) and applying it to the collider(drag and drop on the component) of the moving platform.

There are already multiple answers for this question on the site. Please use the search option for next time. These Answers should get you started. If questions remain after reading those, feel free to post them.

Answer1

Answer2

Answer3

Here is a video that allows you too get multiple objects to stick to a platform while allowing you to interact and manipulate them all you want. - YouTube