Question by
madman_118 · Aug 27, 2015 at 12:09 PM ·
unity 5scripting problemmonodevelopdragging
I have this Drag script it works fine but i want it so when you click and drag it follows the mouse like a trail instead of being underneath it, can someone help me please thanks.
using UnityEngine; using System.Collections;
[RequireComponent(typeof(BoxCollider2D))]
public class Drag : MonoBehaviour { private Vector3 screenPoint; private Vector3 offset;
void OnMouseDown() {
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
Comment
With your solution, I think your offset would always have a length of 0. If you're dragging an object, but the mouse stops moving, should the object catch up to the cursor, or should it slow down before it gets there?
Your answer
Follow this Question
Related Questions
Unity Monodevelop script missing all its lines entirely 0 Answers
Help me please... I have this script but its doesn't drag objects 0 Answers
Cannot get intellisense to work in monodevelop for Unity (C# on windows). 1 Answer
How to take user input, convert it integer, and print it on screen? 1 Answer