- Home /
Question by
Fluq · Oct 24, 2021 at 03:54 PM ·
drag objects
How can I drag an object at specific distances?
Hello Im trying to make the red circle levers move in certain Y distances
I tried something like this but; . public class DragObject : MonoBehaviour {
public GameObject Lever;
Vector2 leverPos, currentLevPos;
float distanceY;
private void FixedUpdate()
{
currentLevPos = transform.position;
leverPos = Lever.transform.position;
distanceY = Mathf.Abs(currentLevPos.y - leverPos.y);
}
void OnMouseDrag()
{
if (distanceY<=0.5 && distanceY>0)
{
Vector2 curPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector2(transform.position.x, curPosition.y);
}
}
} It stays at the distance that I want, but then I cannot move my object Is there any better solution for this ?
ss1.png
(20.1 kB)
Comment
Your answer
Follow this Question
Related Questions
How to drag an object with touch. 1 Answer
Dragged Object, When it Collides with Another Object, Will Go Back to Initial Position 0 Answers
Cant drag player into Network transform child 1 Answer
Object not moving with ground with physics material. 1 Answer
Game object moving in opposite direction instead of same direction while dragging it 0 Answers