- Home /
Question by
hedgy134117 · Jan 23, 2020 at 08:16 PM ·
3dmousebeginnerdrag
ScreenToWorldPoint changing in small increments
I am attempting to make a simple scene where the user can drag objects around in a 3d space. The objects will stay the same on the y axis but move on the x and z based on where the mouse is. This is what I have so far.
private void Update()
{
bool isDragging = Input.GetMouseButton(0);
if (isDragging == true)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
CatDrag(hit.transform.gameObject);
}
}
}
private void CatDrag(GameObject obj)
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
obj.transform.position = new Vector3(mousePos.x, obj.transform.position.y, mousePos.z);
}
This partially works but when the user moves their mouse the object barely moves. I'm rather new to mouse interaction in a 3D space so any and all assistance will greatly help.
Comment
Your answer
Follow this Question
Related Questions
Drag Object Via Axis Handles 0 Answers
Mouse/Wacom stylus drag not working. 1 Answer
How to calculate angles and apply to rotate an object. 1 Answer
3D camera smooth rotation and mouse wheel zoom 2 Answers
mouse drag problem 1 Answer