Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to make a dragable object in unity2D

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;
	}
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: {} is this used for code blcoks in c# 
Csharp :: axwmp balance c# 
Csharp :: how to store more precise data then double c# 
Csharp :: Considerando um projeto C# e o uso do Entity Framework, após realizar uma alteração em um registro, qual o método que deve ser chamado para salvar as 
Csharp :: Exit string qoutes c# 
Csharp :: unity rotate vector around point 
Csharp :: unity get velocity of gameobject 
Csharp :: c# how to output in between 0 - 100 in an int array 
Csharp :: wpf rounded image 
Csharp :: How to Create Hint, PlaceHolder Text, Watermark In a TextBox vb.net 
Csharp :: c# wirite to csv 
Csharp :: c# console writeline array 
Csharp :: unity how to end a game with esc 
Csharp :: c# thread wait 
Csharp :: c# add 1 
Csharp :: visual studio windows form exit button 
Csharp :: loan calculator using windows forms in c# code 
Csharp :: unity iterate all child objects 
Csharp :: c# writteline 
Csharp :: wpf yes no message box exit 
Csharp :: how to convert iformfile to byte array c# 
Csharp :: wpf update listview itemssource 
Csharp :: what type of variable is true or false in c# 
Csharp :: get distinct from datatable c# 
Csharp :: csharp check if env is development 
Csharp :: remove first character in a string c# 
Csharp :: delayed function unity 
Csharp :: merge point of two list 
Csharp :: c# update value in a json file 
Csharp :: loop over enum values 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =