Question by
LouisDreio · Feb 13, 2016 at 07:10 PM ·
2dscript.drag
2D drag script Need Help!
Hello, I am currently developing a game but I need help with something. Is there any script that allows you (the user) to click/tap on an object and drag it around? I am developing this for the I Phone so any help would be greatly apprenticed.
Comment
Answer by Zoogyburger · Feb 13, 2016 at 08:09 PM
You should not ask people to write your code here. Rather, try to write your own and post the errors you might receive. Here is an example script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider))]
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;
}
}
Your answer
Follow this Question
Related Questions
Help with spawning Prefabs that go towards a player 2 Answers
missing cinemachine Kit tools tutorial menu 0 Answers
2D Drag and Drop, but not when slot is already full 0 Answers
Smooth Drag 0 Answers
Unity 2D Touch drag specific Object 0 Answers