I have a 2D game with a player character and an object controlled by the mouse via MovePosition and therefore they’re both rigid bodies, so I can’t attach them via parenting. Is there anything I can do, whether it be changing the object movement script or finding a method other than parenting to get the player to move with the moving platform?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DraggingObject : MonoBehaviour
{
private Vector3 mousePos;
public float moveSpeed = 1.0f;
public Rigidbody2D rb;
Vector2 position = new Vector2(0f, 0f);
bool dragging = false;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
position = Vector2.Lerp(transform.position, mousePos, moveSpeed);
}
private void FixedUpdate()
{
if (dragging)
{
rb.MovePosition(position);
}
}
private void OnMouseDown()
{
dragging = true;
}
private void OnMouseUp()
{
dragging = false;
}
}