- Home /
How to touch, move and rotate 3D objects in XZ?
Hi, I'm new to coding and just started creating some POC.\
I'm new to coding and been searching for assets/ tutorials past 2 days and completely stuck in moving forward.
Can anyone please help me write a Code to Touch, Drag, Move and Rotate the 3D objects?
move: The 3D object should move only in X, Z position keeping the Y position locked.
rotate: It's great if the user can rotate the object when 2 fingers are swiped on the object. Or, it is even greater if when the user rotates the world space UI button( placed below the 3D object).
For testing, it's great if the code works for desktop too.
That's such a general question that the only answer can be: RTF$$anonymous$$.
You need a collider on a 3D object to make it sensitive for collisions (even with the mouse / raycast). Read about colliders, Physics
to do raycasts and the Input
system. For positioning and rotating read the manual about the Transform
component.
Learn your lesson, write code and come back with a particular problem providing your code so far that is not working as expected. I'm sure then you will get help.
Here is the code. This moves in XY position. and I'm unable to move it in XZ.
do you help here?
And, I know and have already tried as you told. If you can help solve this, you can. btw, I'm being polite.
private Vector3 screenPoint;
private Vector3 offset;
void On$$anonymous$$ouseDown()
{
Debug.Log("mouse down");
Vector3 pos = new Vector3 (transform.position.x, 0, transform.position.z);
screenPoint = Camera.main.ScreenToWorldPoint(transform.position);
offset = transform.localPosition - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
Debug.Log (offset);
}
void On$$anonymous$$ouseDrag()
{
Debug.Log("mouse drag");
Vector3 curPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
transform.localPosition = Camera.main.ScreenToWorldPoint (curPosition) + offset;
Debug.Log (Camera.main.ScreenToWorldPoint (curPosition) + offset);
}
Answer by anthot4 · May 29, 2018 at 09:15 AM
Look at Input.GetTouch in the unity documentation.
Drag: You need to draw a raycast from the camera through the users touch position and check if that raycast hits the collider on the gameobject you want to move/drag/rotate. Then code it to check if the user moves their touch position the raycast it hitting a gameobject, if it is then move the gameobject to the position of the users touch position. If the user lifts their finger off the screen stop this process.
Rotate: It depends if you need the angle in radians or degrees for the ui or another part of your game for the best way to do this. Look at transform.eulerangles, quaternion,lookrotation and quaternion.slerp.
Hi, Thanks for the reply.
I've covered the basics :) void On$$anonymous$$ouseDown() { Debug.Log("mouse down"); Vector3 pos = new Vector3 (transform.position.x, 0, transform.position.z); screenPoint = Camera.main.ScreenToWorldPoint(transform.position); offset = transform.localPosition - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); Debug.Log (offset); }
void On$$anonymous$$ouseDrag()
{
Debug.Log("mouse drag");
Vector3 curPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
transform.localPosition = Camera.main.ScreenToWorldPoint (curPosition) + offset;
Debug.Log (Camera.main.ScreenToWorldPoint (curPosition) + offset);
}
Here is the code. This moves the object in XY Position., but I'm unable to solve it to move in XZ positions. I'm really new to coding, any help to make it move in XZ would be awesome.
Where you have "Input.mousePosition.y" change that to "0" and I don't know what value your offset is but make that have a y value of 0 too. Also, I have read its better to raycast in the update function rather than using the "On$$anonymous$$ouse" functions as these are imprecise and don't give you as much control as raycasting.
Sorry, this is the code..
The offset is to check the mouse position and drag it from there..
void On$$anonymous$$ouseDown()
{
Debug.Log("mouse down");
Vector3 pos = new Vector3 (transform.position.x, 0, transform.position.z);
screenPoint = Camera.main.ScreenToWorldPoint(transform.position);
offset = transform.localPosition - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
Debug.Log (offset);
}
void On$$anonymous$$ouseDrag()
{
Debug.Log("mouse drag");
Vector3 curPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
transform.localPosition = Camera.main.ScreenToWorldPoint (curPosition) + 0;
Debug.Log (Camera.main.ScreenToWorldPoint (curPosition) + offset);
}
and, i tried making them 0 as you told, the touched object now moves only in X