Unity 3D Drag Move Object
public class DragScript : MonoBehaviour{
public GameObject player;
void Update(){
if(Input.GetMouseButton(0)) {
float distanceToScreen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 posMove = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
Input.mousePosition.y, distanceToScreen));
float nextX = player.transform.position.x + posMove.x;
Debug.Log(nextX);
Debug.Log("player: "+player.transform.position.x);
if(nextX <= 5.0 && nextX >= -5.0) {
player.transform.position = new Vector3(posMove.x, transform.position.y, 0);
} // Object Move Limited Android Screen
}
}
}
public class DragScript : MonoBehaviour{
public GameObject player;
void Update(){
if(Input.touchCount != 0) {
Touch t = Input.GetTouch(0);
if(t.phase == TouchPhase.Moved) {
float distanceToScreen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 posMove = Camera.main.ScreenToWorldPoint(
new Vector3(t.position.x, t.position.y, distanceToScreen));
float nextX = player.transform.position.x + posMove.x;
if(nextX <= 5.0 && nextX >= -5.0) {
player.transform.position = new Vector3(posMove.x, transform.position.y, 0);
}
}
}
}
}
Unity3D How to Unity Ads reward video in Android ( Google Android Unity 광고 개시 , 보상형광고편) (0) | 2019.02.13 |
---|---|
Unity 화면크기 가져오기. (0) | 2019.01.30 |
Unity3D Android Logcat (0) | 2019.01.18 |
how to change script editor in Unity (0) | 2019.01.08 |
Unity Drag Camera Movement (0) | 2018.10.25 |
Unity RayCast 사용 ( 마우스 ) code (0) | 2018.10.25 |