Rigidbody and Polygon Colliders 2D on MouseDrag

edited in Projects
Hi there.
I am pretty new to coding in Unity and I am having an issue with Rigidbody and Colliders2D in my coding. The character I have, has both of them added as Added Components – Physics 2D.
It works nicely with the 2D Polygon Collider surface until the mouse picks it up and the moves it around.
It then disregards the Polygon Collider surface and moves right through it until it is dropped at which time it then recognizes the surface Polygon Collider again.
I have tried adding in the Rigidbody in the code on MouseDrag but it doesn’t want to work.

Comments

  • Before Mouse Drag
    bug1.JPG
    473 x 369 - 28K
  • on Mouse Drag
    BUG2.jpg
    478 x 314 - 23K
  • edited
    You're SETTING the location of the object instead of moving it by physics. If you don't move something by physics, it's going be set exactly where you tell it to, with no regards for collision to anything between where it's from and where it's being told to go.

    You need to move the object to the destination via physics.

    A more advanced way of doing it is with a PID system which I coincidentally was just asking about in another post: http://makegamessa.com/discussion/5859/physics-pid-system-or-something-that-tracks-real-life-movement-well#latest

    For a simple way to do it this should work:
    https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html
    Thanked by 1Elyaradine
  • Thanks.... I think it may be the Kinematic part that should be Dynamic according to the Unity link.... but the script would not accept a change from:
    rb.isKinematic
    to
    rb.isDynamic
  • isKinematic and isDynamic can be set right on the rigidbody from the Inspector, but I doubt that has anything to do with your problem - as long as you're using .transform.position = blah to set coordinates, the movement WILL NOT interact with the physics system.
  • There is no isDynamic. It's rb.isKinematic = true or rb.isKinematic = false.
    Thanked by 1Tuism
  • Thanks
    Elyaradine - Setting it to true or false makes no difference.
    Tuism - If I remove transform.position = objPosition; the character can not be moved with the mouse.
  • edited
    Have you tried the MovePosition method that Tuism linked?

    Making sure isKinematic is off allows the object to be driven by physics (and therefore be prevented from moving somewhere if it collides with something). Using MovePosition tells the physics engine to move your rigidbody instead of setting the transform directly. Setting the transform directly ignores physics, hence disregarding your colliders.

    It'd be something like this:
    rb.MovePosition(objPosition);
    Thanked by 1dark_seth
  • Thanks. Will give it a try.
  • I found a potential solution on Youtube to the problem. I will check it works and let you know. Thanks again for your help.
  • Did you try rb.MovePosition()?
    Thanked by 1dark_seth
  • Hi there Tuism. Thanks.
    I am sure it is the rb.MovePosition().
    I have tried various code in the () but keep getting error messages. Any thoughts

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MouseDrag : MonoBehaviour {

    Rigidbody2D rb;

    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    }

    void OnMouseDrag()
    {
    rb = GetComponent<Rigidbody2D>();

    rb.MovePosition( ------------------);

    Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z + transform.position.z);

    Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);

    transform.position = objPosition;

    rb.isKinematic = true;
    }

    private void OnMouseUp()
    {
    rb.isKinematic = false;
    }
    }
  • Hi Elyaradine - Thanks.
    The Character is never driven by physics, it is moved only by drag and drop function. Hence the problem when it doesn't want to recognize the Polygon Collider during dragging.
  • edited
    I haven't tried your code, but I can see some problems already.

    It has the MovePosition code with (-----------------); <-- what's that supposed to mean? You need to feed it the position you want it to go to.

    Replace the "transform.position = objPosition" with the MovePosition code.

    And rb.isKinematic = true must go, that would make it ignore physics. MovePosition moves the rigidbody with physics.

    Also rb = GetComponent<Rigidbody2D>(); running every frame while you drag is unnecessary and very slow, you already got the rigidbody reference into rb during Start().
  • Thanks for your help Tuism
    It seems to be working OK with those changes and a slight modification here and there.
  • Cool, the most important thing is for you to understand what each line of those codes mean so that you can apply the learning to future stuff. Good luck!
Sign In or Register to comment.