Reputation: 113
I'm using the script below to move an object (on X and Z axis) smoothly with mouse movement, it works perfectly on the world's axis, I mean when the object's rotation is (0,0,0).
But how can I make the object move on its local axis (I mean when its X rotation is -20, its Z movement should be progressive)?
Script:
private Vector3 screenPoint;
private Vector3 offset;
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0) || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
if (Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0) || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = new Vector3((Camera.main.ScreenToWorldPoint(curScreenPoint).x + offset.x), 2, (Camera.main.ScreenToWorldPoint(curScreenPoint).z + offset.z));
float newX = Mathf.Clamp(curPosition.x, -3f, 3f);
float newZ = Mathf.Clamp(curPosition.z, -6f, 1.5f);
transform.position = Vector3.Lerp(transform.position, new Vector3(newX, transform.position.y, newZ), 10 * Time.deltaTime);
}
}
Upvotes: 0
Views: 603
Reputation: 113
Okey I found the answer, and it was simple:
Upvotes: 1