- Home /
Mobile select and move Object with offset
Hey! I am working on a mobile project and trying to move objects by double clicking on them and then dragging them, while selected and moving the object is supposed to have a little offset on the y-axis to make it clear which object is handled. So far Clickslection and Doubleclick event is working, the movement is also working and the offset but only the first time I select an object it gets the offset. When I select and move it again it just moves around but doesn't get the y-axis offset. The chosen Objects are Rigidbodies & Colliders, so they fall back on their own. But I can't find why the Y-Offset is only applied once per Object-Lifetime.
if (Input.GetTouch(0).phase == TouchPhase.Ended && _doubleClickActivated == true)
{
_selectedObject.transform.GetChild(0).gameObject.GetComponent<Rigidbody>().isKinematic = false;
_selectedObject.transform.GetChild(0).gameObject.GetComponent<Rigidbody>().detectCollisions = true;
_selectedObject.layer = 0;
if(_selectedObject.transform.childCount > 0)
{
_selectedObject.transform.GetChild(0).gameObject.layer = 0;
}
if(_selectedObject.transform.GetChild(0).childCount > 0)
{
_selectedObject.transform.GetChild(0).GetChild(0).gameObject.layer = 0;
}
_selectedObject = null;
_objSelected = 0;
_objectFix = true;
_doubleClickActivated = false;
}
if (_objectFix == false)
{
if(_objSelected == 1)
{
RaycastHit hit;
Ray ray = _cam.ScreenPointToRay (touch.position);
if (Physics.Raycast(ray, out hit))
{
_selectedObject.transform.GetChild(0).gameObject.GetComponent<Rigidbody>().isKinematic = true;
_selectedObject.transform.GetChild(0).gameObject.GetComponent<Rigidbody>().detectCollisions = false;
_selectedObject.transform.position = new Vector3(hit.point.x, hit.point.y+0.2f, hit.point.z);
}
}
}
Is the code I came up with for moving the object. Since the Doubletap & Object Detection seems to work fine I assume the mistake lies within that part. All the selection code does is raycast on the finge position, move the hit object to the Raycast ignore layer and save a reference to the selected object.
If anyone could find out what the problem with the offset only applying once that would be awesome :)
EDIT: Oh forgot to mentiont that the code is part of the update function, in case that is relevant.
Your answer
Follow this Question
Related Questions
Selecting objects and limiting movement space 0 Answers
Movement Object 0 Answers
How do I stop an object from cloning when moving? 1 Answer
Rigid Body Movement (Mobile Issue) 3 Answers
My character has stopped moving 0 Answers