- Home /
The question is answered, right answer was accepted
[STILL UNANSWERED] Lock object to mouse position & rotate it about mouse? [C#, 2D]
So I have a rigidbody2d on a component. I want it to move with the mouse like this: ![process][1] How would I do this?
Current Code:
GameObject controller;
public LayerMask hitLayers;
Rigidbody2D rig;
bool selected;
private void Start()
{
controller = GameObject.Find("Controller");
rig = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (selected)
{
Vector3 pos = Input.mousePosition;
pos.z = 10;
pos = Camera.main.ScreenToWorldPoint(pos);
Vector3 dist = pos - transform.position;
Vector3 newPos = pos - dist;
transform.position = newPos;
}
}
private void OnMouseUp()
{
selected = false;
rig.velocity = new Vector2(0, 0);
rig.angularVelocity = 0;
}
private void OnMouseDown()
{
selected = true;
}
But it doesn't move at all. When you click on it, it will stop falling and very slowly move down, and if its already landed, it won't do anything. [1]: /storage/temp/124733-how.png
You could look into hinges. I've personally never experimented with them much, but when you pick up an object, attach a hinge point component to it (or whatever is required). I can't guarantee this will work but if I were to implement this I would start my research there
Answer by Dankey_Kang · Oct 03, 2018 at 02:53 PM
You are going to want to do a simple raycast to pickup the object and use a TargetJoint2D component to have the object anchor based on your mouse position relative to the object and track your mouse position. This is a basic working version. I would look into more raycasting stuff as it is extremely useful to know how to do. Hope this helps!
Attach this script to your box with the rigidbody2D and its box collider.
[RequireComponent(typeof(TargetJoint2D))]
public class Dragable : MonoBehaviour
{
private TargetJoint2D _joint;
void Start ()
{
_joint = GetComponent<TargetJoint2D> ();
if (_joint == null) {
enabled = false;
} else {
_joint.enabled = false;
}
}
public void BeginDrag(Vector2 mouse_pos)
{
Vector2 pos = (Vector2) transform.position;
Vector2 scale = (Vector2)transform.localScale;
Vector2 anchor = (mouse_pos - pos);
if (scale.x == 0) {
anchor.x = 0f;
} else {
anchor.x /= scale.x;
}
if (scale.y == 0) {
anchor.y = 0f;
} else {
anchor.y /= scale.y;
}
_joint.enabled = true;
_joint.anchor = anchor;
UpdateDrag (mouse_pos);
}
public void UpdateDrag(Vector2 mouse_pos)
{
_joint.target = mouse_pos;
}
public void EndDrag()
{
_joint.enabled = false;
}
}
Attach this script to your camera, or just whatever object since it doesn't rely on the object it is attached to but make sure there is only one of them in the scene at a time.
public class BoxDrag : MonoBehaviour
{
private Dragable _drag_target;
void Start()
{
_drag_target = null;
}
void Update ()
{
if (_drag_target == null) {
if (Input.GetMouseButtonDown (0)) {
Vector2 mouse_pos = (Vector2)Camera.main.ScreenToWorldPoint (Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (mouse_pos, Vector2.zero);
if (hit.collider != null) {
Dragable dragable = hit.collider.GetComponent<Dragable> ();
if (dragable != null) {
_drag_target = dragable;
_drag_target.BeginDrag (mouse_pos);
}
}
}
}
if (_drag_target != null) {
Vector2 mouse_pos = (Vector2)Camera.main.ScreenToWorldPoint (Input.mousePosition);
_drag_target.UpdateDrag (mouse_pos);
if (Input.GetMouseButtonUp (0)) {
_drag_target.EndDrag ();
_drag_target = null;
}
}
}
}
Answer by Shallow3 · Sep 18, 2018 at 02:03 PM
When pressing down the mouse shoot a raycast and see if the mouse is over the cube, if so have the cube follow the mouse until you release the mouse. you can mainly do this with simply if statements like: if(Input.GetButton("Fire1")) {
}
How do you raycast? I'm still learning Unity, (only made one crappy game that made less than a buck), and I have never had to use these before.
@Shallow3 I really need you to respond or delete your answer, because if people see that there is an answer then they don't respond any longer.
Hi, I am working on a solution for you. Watch this space! -Greg
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Displaying the y value score,Displaying the score of the Y value 1 Answer
OnMouseDown (Collider other) Not working 1 Answer
Deteriorating force on object? 0 Answers