- Home /
How to translate object using script?
Hello! I have a script adapted from one my tutor gave me, which allows me to rotate and zoom on an object. The script is attached to my main camera and focused on an empty game object which is in the same position as my actual object. My question is: how do I add a functionality to allow me to move the object across the screen as well (ie. translate it on the x,y axes)? I am making an app for Android, so currently the rotate function is with one finger drag, the zoom is pinch, and I would like to be able to add this translate function so that a two finger drag moves the object. I have trawled unity answers and have found nothing and am really struggling to work out in my head how to adapt this script, so any help would be much appreciated!! Cheers.
using UnityEngine; using System.Collections;
[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")] public class DragMouseOrbit : MonoBehaviour { public Transform target; public float distance = 500.0f; public float xSpeed = 0.1f; public float ySpeed = 10.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = 200f;
public float distanceMax = 500f;
public float smoothTime = 2f;
public float zoomSpeed = 100.0f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
public bool detectColliders = false;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void LateUpdate()
{
if (target)
{
if (Input.touchCount == 1)
{
velocityX += xSpeed * (Input.GetTouch(0).deltaPosition.x / Time.deltaTime) * distance * 0.0001f;
velocityY += ySpeed * (Input.GetTouch(0).deltaPosition.y / Time.deltaTime) * 0.0001f;
} else if (Input.GetMouseButton(0) && Input.touchCount == 0)
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
velocityX = Mathf.Clamp(velocityX, -2.5f, 2.5f);
velocityY = Mathf.Clamp(velocityY, -1.5f, 1.5f);
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
//Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
if (Input.touchCount == 2)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
distance = Mathf.Clamp(distance + deltaMagnitudeDiff * zoomSpeed * 0.005f, distanceMin, distanceMax);
} else
{
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomSpeed, distanceMin, distanceMax);
}
if (detectColliders) {
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distanceMin = hit.distance;
}
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
Answer by juna8001 · Jul 02, 2018 at 10:32 AM
Try this:
private void TranslateTarget(Vector2 motion)
{
target.position += transform.right * motion.x + transform.up * motion.y;
}