Unity3d moving, touching

I am going to create an Android application where I move the gameObject to the X and Z cords, touching, the gameObject should move right, left, up or down. I wrote this script, which belongs to gameObject, where I check the touch of Raycast.

Do you know of any other method of moving an object by touching?

#pragma strict var hit = new RaycastHit(); function Start () { } function FixedUpdate () { for (var i :int = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(0).phase == TouchPhase.Moved ) { var touchDeltaPosition:Vector2 = Input.GetTouch(i).deltaPosition; var touch_pos : Vector3 = new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y , 0); var ray : Ray = Camera.main.ScreenPointToRay (touch_pos); Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow); if (Physics.Raycast (ray, hit)) { Debug.Log(hit.transform.tag); if(hit.transform.tag == "Cubes") { if(Mathf.Abs(Input.GetTouch(i).deltaPosition.x) > Mathf.Abs(Input.GetTouch(i).deltaPosition.y)) { if(Input.GetTouch(i).deltaPosition.x > 0) { transform.Translate(0,0,1); // 'right'; } else { transform.Translate(0,0,-1); //GUItest.text = 'left'; } } else { if(Input.GetTouch(i).deltaPosition.y > 0) { transform.Translate(-1,0,0); // 'up'; } else { transform.Translate(1,0,0); // 'dawn'; } } } } } } 

}

+4
source share
3 answers

take the touch position, save it and compare with the current touch position calculate the delta and move the object

  if(Input.GetMouseButtonDown(0)) { //print ("up..."); mousePositionXYZ.y= Input.mousePosition.y; doit=true; } if(doit) { listGO=GameObject.Find("Main Camera"); if(Input.mousePosition.y > mousePositionXYZ.y ) { listGO.transform.position.z -=Input.mousePosition.y *Time.deltaTime *0.010; print ("up..."); } else if(Input.mousePosition.y < mousePositionXYZ.y ) { listGO.transform.position.z +=Input.mousePosition.y *Time.deltaTime*0.010; print ("down..."); } } if(Input.GetMouseButtonUp(0)) { mousePositionXYZ.y= Input.mousePosition.y; doit=false; } 
+1
source

you can use this to move objects with touch

 v3Pos = Vector3(touchDeltaPosition.x -50, touchDeltaPosition.y + 50, distance); transform.position = Camera.main.ScreenToWorldPoint(v3Pos); 
0
source

The script from The Game Contriver does the same using Vector3.Lerp and at a constant speed regardless of the difference between the two points.

 using UnityEngine; using System.Collections; public class TapToMove : MonoBehaviour { //flag to check if the user has tapped / clicked. //Set to true on click. Reset to false on reaching destination private bool flag = false; //destination point private Vector3 endPoint; //alter this to change the speed of the movement of player / gameobject public float duration = 50.0f; //vertical position of the gameobject private float yAxis; void Start(){ //save the y axis value of gameobject yAxis = gameObject.transform.position.y; } // Update is called once per frame void Update () { //check if the screen is touched / clicked if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))) { //declare a variable of RaycastHit struct RaycastHit hit; //Create a Ray on the tapped / clicked position Ray ray; //for unity editor #if UNITY_EDITOR ray = Camera.main.ScreenPointToRay(Input.mousePosition); //for touch device #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8) ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); #endif //Check if the ray hits any collider if(Physics.Raycast(ray,out hit)) { //set a flag to indicate to move the gameobject flag = true; //save the click / tap position endPoint = hit.point; //as we do not want to change the y axis value based on touch position, reset it to original y axis value endPoint.y = yAxis; Debug.Log(endPoint); } } //check if the flag for movement is true and the current gameobject position is not same as the clicked / tapped position if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)){ //&& !(V3Equal(transform.position, endPoint))){ //move the gameobject to the desired position gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1/(duration*(Vector3.Distance(gameObject.transform.position, endPoint)))); } //set the movement indicator flag to false if the endPoint and current gameobject position are equal else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) { flag = false; Debug.Log("I am here"); } } } 

You can find the entire article at the following link Move To Tap / Click Position - Game Contriver

0
source

Source: https://habr.com/ru/post/1487600/


All Articles