I am trying to drag an object with iOS touch. It seems to be almost working, but it does not hold onto the object when I begin to drag my finger across the screen.
if(Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Moved){
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray)){
touch=ray.origin;
enemy.position=touch;
}
}
In other words you should drag it until you separate it if you become effective once
if(Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Moved){
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray)){
dragEnable = true;
}
if( dragEnable ){
this.gameObject.transform.position=Input.GetTouch(0).position;
}
} else {
dragEnable = false;
}
kevork
2
Use the hitInfo from the Raycast instead of Ray.origin as the world position of the object.
Could you elaborate further on this? This is what I have currently:
if(Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Moved){
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if(Input.GetTouch(0).phase==TouchPhase.Moved){
if (Physics.Raycast(ray)){
this.gameObject.transform.position=ray.GetPoint(100);
}
}
}
This does grab the object, but it seems to fall out of range when moving my finger around