- Home /
move objects by touch position
basically i have 5 objects all parented to a root object, and when i click on one of the children it moves the parent by the touch position on the screen, but what i can't work out is how to move the parent from the current position it is at, the parent will always move back to the touch position.
so i need to move the object + the touch position but not so that it keeps adding movement
To make it simpler i want ti move object a position.x + touch position.x. But not to keep adding the touch position to the object.
At the moment its in an update so it keeps moving by touch position when i only want to move it by the how much the touch position has moved. So if touch moves by 2 the object a will move by two not two every frame.
Its got to happen whilst my finger is moving though not after
hmm it would be easy to say if you have posted some code snippet you are using!
Gameobject.find("OBjectA").transform.position.x = touch.position.x
If i use plus it will keep adding every frame i just need it to he added once
Answer by flamy · Jul 31, 2012 at 11:22 AM
Im still not clear about what exactly you want to do but i guess you should be using Events from unity..
I guess you want to do something like the below snippet
void OnGUI()
{
if(Event.current.type == EventType.mouseDrag)
{
GameObject.find("OBjectA").transform.position +=(Vector3)Event.current.delta;
}
}
delta variable from Event will return the value change from last frame (when mouse drag is happening). I guess that is what you are trying to do?!! but even then that is not a proper way to do. since touch.Position or Event.current.delta or Input.mousePosition, all these will be in screen co-ordinate, you should be converting it to world co-ordinate value, which would take considerable amount of work.
I think the best would be converting the touch or mouse position to world coordinate. and add an offset to it depending upon the difference between the object position and touch down position. The below is jus a sample i have checked. I hope i might give you some idea to continue further.
Vector3 offset;
Transform _object;
void Start () {
_object=GameObject.Find("OBjectA").transform;
}
void OnGUI()
{
if(Event.current.type == EventType.mouseDown)
{
offset = _object.position- Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, _object.position.z-Camera.main.transform.position.z));
}
if(Event.current.type == EventType.mouseDrag)
{
_object.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, _object.position.z- Camera.main.transform.position.z)) + offset;
}
}
got it i will give it a go, sorry i wasn't at my computer to give a proper script snippet
here is a better snippet of code, I'm working in java, but i've been $$anonymous$$ching myself cSharp as well so its fine to answer in cSharp.
var touchPos : Vector3;
touchPos = Camera.mainCamera.ScreenToWorldPoint(touch.position);
if(touch.phase == TouchPhase.$$anonymous$$oved){
object.collider.enabled = true;
moveObjects.transform.position.x = touchPos.x;
its just the bottom line, oh and its inside an update function, so it keeps adding the touch position if i use + every frame.
sorry I'm terrible at explaining
ok yh this is what i was looking for, works fine, the object will move anywhere now without constantly adding the amount of touch position, to it. its just 5 added to its current position not 5 every dam frame.
Your answer
