- Home /
Question by
Maniecoetzee · Nov 24, 2013 at 09:16 PM ·
axisslideconstantyonmousedrag
Sliding cube around on x/z axis with a constant y-axis
Hi,
I have tried several ways of doing this but I cannot get it right. I'm basically sliding a cube on a plane. The plane is just there for visual effect. I don't want to use a rigid body, etc. I'm using the following code for the dragging:
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 dragPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
dragPosition = Camera.main.ScreenToWorldPoint(dragPosition) + offset;
dragPosition.y = 2.0f;
transform.position = dragPosition;
}
The cube needs to slide around the screen but remain of a y value of 2.0f. The cube moves on the X and Z axis but I want the y-axis to stay constant on 2.0f, but it goes out of sync with the cursor. Can anyone give me a pointer in the right direction?
Comment
Your answer
Follow this Question
Related Questions
Rotate and Move towards Mouse Point 0 Answers
How to lock the y axis in camera script? 2 Answers
LookAt without Y axis 2 Answers
Lock the Camera's Y Rotation Axis? 0 Answers