Rigidbody and Polygon Colliders 2D on MouseDrag
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.
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
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
rb.isKinematic
to
rb.isDynamic
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.
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:
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;
}
}
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.
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().
It seems to be working OK with those changes and a slight modification here and there.