Hey ppl.
I am trying to drag my game object using iphone touch. I am having the problem that my object is dragging wherever on the screen I touch, I want it to drag only when placing my finger on object. Here is my code:
void Update () {
foreach (Touch touch in Input.touches) {
Ray ray = Camera.main.ScreenPointToRay (touch.position);
if(touch.phase == TouchPhase.Began && Physics.Raycast(ray, out hit)) {
if(hit.collider.gameObject.tag == "Player")
{
//translate the cubes position from the world to Screen Point
screenSpace = Camera.main.WorldToScreenPoint(transform.localPosition);
//calculate any difference between the cubes world position and the mouses Screen position converted to a world point
offset = transform.localPosition - Camera.main.ScreenToWorldPoint(
new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
Debug.Log("Finger has been touched...");
}
}
if(touch.phase == TouchPhase.Moved && Physics.Raycast(ray, out hit,1000)) {
if(hit.collider.gameObject.tag == "Player")
{
//keep track of the mouse position
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//convert the screen mouse position to world point and adjust with offset
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
//update the position of the object in the world
transform.position = curPosition;
}
}
}//for
Please help me with this
When I want to move a specific object, I create a variable to hold that object and assign it in the Editor, and then directly update the transform.position of that object to the location of my touch when phase=moved in the Update() function, that way I am directly calling the object to be moved; I am fairly certain using the gameObject.tag method has some overhead as Unity will have to ‘find’ your object, then test for a collider hit:
var joyButton : GUITexture; //the actual object you are dragging
var joyOrient : GameObject; //this should be the same as joyButton, used for location purposes only
private var orientPosition : Vector3;
private var storedID : int;
function Awake () {
orientPosition = joyOrient.transform.position; //set orient to initial location of joyOrient object
} //End Awake
function Update () {
for (var event : Touch in Input.touches) { //check for touches
var JoyHitTest = (joyButton.HitTest(event.position));
if (event.phase == TouchPhase.Began || event.phase == TouchPhase.Moved) { //if phase is Began or Moved
joyTouchID = event.fingerId; //get ID of latched touch
if(JoyHitTest){ //and if you touched the joystick thumb
latched = true; //touch is latched to stick
storedID = joyTouchID; //store ID of finger used to initiate this touch
} //End if
} //End if
if (event.phase == TouchPhase.Ended && event.fingerId == storedID){
latched = false;
} //End If
if (latched==true){
joyButton.transform.position.x = (event.position.x/1024); //set position of thumb to finger location on iPad screen
joyButton.transform.position.y = (event.position.y/768); //must change this so resolution is variable driven
joyButton.transform.position.z = 0; //z axis is always zero since we only care about 2D movement here
} //End If
if (latched==false){
joyButton.transform.position = orientPosition; //return joystick thumb to the orient location
}//End If
} //End for
} //End Update
This will allow you to drag a GUITexture (joyButton) around the screen. Because touch location and object position use two different coordinate systems, we have to manually convert from one to the other, hence the division. You will want to adjust the 1024 and 768 to the resolution of your device; I generally make those numbers a variable like divisorX and divisorY so I can detect the device the end user is operating and adjust the numbers accordingly. If you implement this and you still have lag in the simulator, then it is because of the simulator; this code is tested and runs at 30FPS on a first gen iPhone. You should be able to make this work with a GO by changing the typing of joyButton
to GameObject
. Let me know if this helps.