Question by
bommba · Feb 14, 2016 at 09:17 PM ·
scripting problemdrag-and-drop
Drag and Drop along X and Z-Axis
How can I make a Drag and Drop, where the object doesn´t move along the y-Axis. Online I had found this script: using UnityEngine; using System.Collections;
public class drag : MonoBehaviour {
Vector3 dist;
float posX;
float posY;
void OnMouseDown(){
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
}
void OnMouseDrag(){
Vector3 curPos =
new Vector3(Input.mousePosition.x - posX,
Input.mousePosition.y - posY, dist.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
}
}
But how can I change it, that the Object moves along Z instead Y Axis?
Comment
Can you not just change all of the instances of "y" to "z," then go from there?
Answer by hylmz055 · Jul 13, 2016 at 05:55 AM
Try this one.
public class Drag : BaseClass
{
Vector3 dist;
Vector3 startPos;
float posX;
float posZ;
float posY;
void OnMouseDown()
{
startPos = transform.position;
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
posZ = Input.mousePosition.z - dist.z;
}
void OnMouseDrag()
{
float disX = Input.mousePosition.x - posX;
float disY = Input.mousePosition.y - posY;
float disZ = Input.mousePosition.z - posZ;
Vector3 lastPos = Camera.main.ScreenToWorldPoint(new Vector3(disX, disY, disZ));
transform.position = new Vector3(lastPos.x, startPos.y, lastPos.z);
}
}
thank you for this code, but when I add this to my object it's effecting only its in world pivot, my objects are rotated in a different way. Is there a possible way to use this code with its local pivot