Hi, I’m working on a project that requires the user click on objects and move them around. Simple stuff.
I have created an example project which shows 2 examples of the same functionality, but they don’t behave the same.
The Scenario:
I have created a blue cube and a green cube. They can be clicked and dragged around on the X and Y axis. When the mouse is released the objects snap back to the origional position.
The Scene Hierarchy:
- ParentWithScript - This object has the MoveXY_Script attached to it. Nothing more.
- BlueCube - This is a child of the ParentWithScript object and is just a blue cube with the tag “Cube”.
- GreenCube - This object has the MoveXY2_Script attached to it and is a cube.
The Problem:
When the green cube is moved it follows the mouse curser on the X and Y axis perfect.
When the blue cube is moved it get’s offset from the mouse pointer, though you can only really tell when looking at the objects from an angle.
Here is a video displaying the problem:
Now this is strange because the scripts are basically the same apart from the way the events are fired (One is raycast and the other is OnMouseDown event) and the the way the object that is being interacted with is referenced (One is referenced by the raycast and the other is referenced by gameObject).
Here is Green Cube script (This is the script for the object without a parent which behaves how I would expect):
public class MoveXY2_Script : MonoBehaviour
{
bool canMove;
void OnMouseDown()
{
canMove = true;
}
private void OnMouseUp()
{
canMove = false;
}
private void Update()
{
if (canMove)
{
float distance_to_screen = Camera.main.WorldToScreenPoint(transform.position).z;
Vector3 mouseTo3D = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
Vector3 newPos = new Vector3(mouseTo3D.x, mouseTo3D.y, gameObject.transform.position.z);
gameObject.transform.position = newPos;
}
else
{
transform.position = new Vector3(0, 0, 0);
}
}
}
Now here is the Blue Cube script (This is the script for the object that doesn’t behave as I would expect):
public class MoveXY_Script : MonoBehaviour
{
bool canMove;
GameObject clickedObj;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.tag == "Cube")
{
clickedObj = hit.collider.gameObject;
canMove = true;
}
}
}
else if (Input.GetMouseButtonUp(0))
{
canMove = false;
}
if (canMove)
{
float distance_to_screen = Camera.main.WorldToScreenPoint(transform.position).z;
Vector3 mouseTo3D = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
Vector3 newPos = new Vector3(mouseTo3D.x, mouseTo3D.y, clickedObj.transform.position.z);
clickedObj.transform.position = newPos;
}
else
{
if (clickedObj != null)
{
clickedObj.transform.position = transform.position;
}
}
}
}
Now in an ideal world I need to be able to move the blue cube around but I need it to actually stay where the mouse pointer is and not be offset. The parent in this case is a necessity because of the way the project has been set up.
So does anyone know why the blue cube is not moving to where the mouse is on the x and y axis?
And does anyone know how I can make it so the blue cube properly moves to the mouse curser while still being a child of the parent?
Many thanks! This is driving me mad.