Question by
Micordie · Aug 28, 2020 at 02:33 AM ·
mousepositionhingejoint2d
Trying to create script that drags an object connected by a hinge
So, I am trying to write a script where an object at the end of a chain of hinges follows the mouse. The problem is that when I use seemingly anything that involves "transform.position = ...", the object I am trying to drag completely disconnects from the chain. So is there anything I can do to make this work?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowMouse : MonoBehaviour
{
void Update()
{
float xMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
float yMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
new Vector3 mousePos = (xMousePos, yMousePos, 0)
transform.position = Vector3(xMousePos, yMousePos, 0);
}
}
I have tried other things, but this is essentially what I have been doing. Thanks.
Comment