Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Player not attaching to mouse controlled platforms

Discussion in '2D' started by SnakeyJake15, Oct 20, 2023.

  1. SnakeyJake15

    SnakeyJake15

    Joined:
    Oct 3, 2023
    Posts:
    2
    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?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class DraggingObject : MonoBehaviour
    7. {
    8. private Vector3 mousePos;
    9. public float moveSpeed = 1.0f;
    10. public Rigidbody2D rb;
    11. Vector2 position = new Vector2(0f, 0f);
    12. bool dragging = false;
    13.  
    14.     private void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody2D>();
    17.     }
    18.     private void Update()
    19.     {
    20.         mousePos = Input.mousePosition;
    21.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    22.         position = Vector2.Lerp(transform.position, mousePos, moveSpeed);
    23.     }
    24.     private void FixedUpdate()
    25.     {
    26.         if (dragging)
    27.         {
    28.             rb.MovePosition(position);
    29.         }
    30.     }
    31.     private void OnMouseDown()
    32.     {
    33.         dragging = true;
    34.     }
    35.     private void OnMouseUp()
    36.     {
    37.         dragging = false;
    38.     }
    39. }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,354
    Why do you need physics on a platform you are only moving via mouse drag? I'd say take the rigidbody off the platform and then parent the player like you mention.
     
  3. SnakeyJake15

    SnakeyJake15

    Joined:
    Oct 3, 2023
    Posts:
    2
    I want the platform to be affected by gravity and collisions, plus removing the Rigidbody would disable the MovePosition command required to make it move.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,354
    There are like 5 different ways to move an object, MovePosition is just one. Collisions are handled via colliders and as long as you have a rigidbody and collider on your player, it is handled.

    I would think a bit more about how gravity is affecting the platforms. Are the platforms constantly falling (or constantly pulling from a certain direction)? Cause thats what gravity does, a pull from a direction.