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. Dismiss Notice

Question how do i drag an object that interacts with other objects?

Discussion in '2D' started by unity_005946E12C5F7D78583B, Jul 10, 2023.

  1. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    hello! im making a little game to learn unity and i want the player to be able to move the platforms with the mouse cursor, i have a basic script for this and it works pretty well but, the way the script is made, it makes the platform to just go through the floor wich i dont want, i want the platform to be able to be dragged by the cursor but with the detail that it cant go through another platforms or things like the floor or the walls

    how can i do this?
     
  2. Reid_Taylor

    Reid_Taylor

    Joined:
    Oct 9, 2019
    Posts:
    57
    I think the best way to do this would be to use a kinematic rigid body. Just add a rigid body to your platform, turn off gravity on the rigid body, turn on kinematic on the rigid body, and make sure a nice fitting collider is on the platform as well. And obviously, you need colliders on anything that the platform should not go through.

    After adding a rigid body you might have to play around with how you move the platform in script a bit. (but maybe not :) it depends on how you have it set up already)
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
  4. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    The layers that can be dragged.

    No idea what you mean but know, they are not tutorials.
     
  6. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    the code isnt working, the target joint is created but the platform isnt moving, what do i need to make it work?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    I have no idea what you're doing wrong so I cannot tell you how to make it work.
     
  8. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    can i download the game project?, im gonna see if i can take reference form there
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    What game project? You mean my physics examples? I posted the link above, it's also in my signature!
     
  10. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    ok, after watching your project i manage to make it work, but, random question, is there a way that the platform stays still when im not dragging it? like, a way that you could drag the platform and all but the platform itself doesnt rotate or moves when your not dragging it, is that possible?
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    I have no idea what platform you mean. You should clarify what specifically you're asking about really, I'm not looking at what you're looking at.

    But yes, of course anything is possible. When you're not dragging it, stop "it" from rotating.
     
  12. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    ok ive tried a couple of things and it works!
    the dragtarget script does something to the collider? im testing the game and the player just phases thru the platform when not clicked
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    I don't understand you asking, the tiny script is right there in front of you. As I've said, it's not a tutorial but an example.

    Clearly it does nothing to the collider but adds a joint then uses it to drive to the target.
    https://github.com/Unity-Technologi...b/master/Assets/Scripts/Generic/DragTarget.cs

    The description:
    /// Drag a Rigidbody2D by selecting one of its colliders by pressing the mouse down.
    /// When the collider is selected, add a TargetJoint2D.
    /// While the mouse is moving, continually set the target to the mouse position.
    /// When the mouse is released, the TargetJoint2D is deleted.


    Nothing more I can add.
     
  14. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    alright im gonna try some things
     
    MelvMay likes this.
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    Have fun!