How do I make objects move with the moving platform?
I'm very new to Unity and trying to make a 2d platforming game where you solve puzzles using crates and moving platforms. I have made a platform that moves, however when the player or any object with a rigidbody is on the platform it won't move with it and just stays in place.
This is my code for the moving platform:
using UnityEngine;
using System.Collections;
public class PlatformMovement : MonoBehaviour {
public bool activated;
public GameObject player;
private Vector3 posA;
private Vector3 posB;
private Vector3 nextPos;
[SerializeField]
private float speed;
[SerializeField]
private Transform childTransform;
[SerializeField]
private Transform transformB;
// Use this for initialization
void Start () {
posA = childTransform.localPosition;
posB = transformB.localPosition;
nextPos = posB;
}
// Update is called once per frame
void Update () {
Move();
}
public void OnTriggerStay2D(Collider2D other)
{
Transform thingOnPlatform = other.transform;
thingOnPlatform.position = Vector3.MoveTowards(thingOnPlatform.position, nextPos, (speed * Time.deltaTime));
}
private void Move()
{
childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nextPos, speed * Time.deltaTime);
if (Vector3.Distance(childTransform.localPosition,nextPos) <= 0.1)
{
ChangeDir();
}
}
private void ChangeDir()
{
nextPos = nextPos != posA ? posA : posB;
}
}
I'm using Unity 5.4.0f3 if you need to know.
Answer by Adam-Mechtley · Nov 28, 2016 at 09:26 AM
Hi! Right now you are directly setting the transform's localPosition. I'm not sure if it will solve the problem in your case, but you might instead consider using Rigidbody2D.SetPosition (). (Likewise, make sure your moving platform has a Rigidbody2D component with the isKinematic flag to set to true.)
I tried this:
public void OnTriggerStay2D(Collider2D other)
{
Rigidbody2D thingOnPlatform = other.gameObject.GetComponent<Rigidbody2D>();
thingOnPlatform.$$anonymous$$ovePosition(thingOnPlatform.position + GetComponent<Rigidbody2D>().velocity *Time.fixedDeltaTime);
}
I don't know if i did it wrong (I'm very new to unity and program$$anonymous$$g in general) but it didn't work. Ins$$anonymous$$d my character couldn't move at all.
Sorry for the lack of clarity on my part. I meant that you should be moving the platform using Rigidbody2D.$$anonymous$$ovePosition () inside your $$anonymous$$ove () function. I'm not sure exactly what you're trying to do inside of OnTriggerStay2D () though. If your platform has a collider and a kinematic Rigidbody2D, then any non-kinematic Rigidbody2D with a collider sitting on top of it should automatically be moved around via physics.
Your answer
Follow this Question
Related Questions
Move Object in Button Click smooth to position? 2 Answers
Click on object shows menu, but also move it. 0 Answers
Moving moving Object with left/right arrow keys in a circular direction 3 Answers
Why the loop in waypoints in not working when set to true? 0 Answers
How do I move a game object only on the x axis with arrow keys? 1 Answer