Locking Y and Z axis.C#
I have this code that allows me to move objects.I would like to change it that i could only move objects on x axis. THIS IS MY CODE:
private Vector3 screenPoint;
private Vector3 offset;
void Start () {
}
void Update () {
}
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 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
Comment
void On$$anonymous$$ouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
curPosition.y = 0f;
//curPosition.z = 0f;
transform.position = curPosition;
}
Try that
Thanks for the idea. I did it like this:
void Update () {
myPosition = transform.position;
}
void On$$anonymous$$ouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
curPosition.y = myPosition.y;
curPosition.z = myPosition.z;
transform.position = curPosition;
}
But i don't really understand how to prevent the object jumping. I mean whenever i press on any side of the object it jumps towards that direction... :/