Question by
josephbenjones · May 20, 2021 at 02:49 PM ·
scripting problembeginner
Combining Track and Zoom scripts for Mobile
Hello, im new to unity, forgive me if this is a silly question.
Im looking at scripting a way for my camera to track my player and also having a zoom function.
My zoom script looks like this: public class PinchZoom : MonoBehaviour { public Transform targetb; public Vector3 offsetb; public float pitchb = 1f; public float zoomRate = 0.02f;
private Camera cam;
void Awake()
{
cam = GetComponent<Camera>();
}
void Update ()
{
if (Input.touchCount == 2)
{
//Get Touch input position from start/ previous frame
Vector3 aFingerPosition = Input.touches[0].position - Input.touches[0].deltaPosition;
Vector3 bFingerPosition = Input.touches[1].position - Input.touches[1].deltaPosition;
//Get difference between previous touches and current
float prePosition = (aFingerPosition - bFingerPosition).magnitude;
float currentPosition = (Input.touches[0].position - Input.touches[1].position).magnitude;
float difference = currentPosition - prePosition;
if (cam.orthographic)
{
cam.orthographicSize -= difference * zoomRate;
cam.orthographicSize = Mathf.Clamp(cam.orthographicSize, offsetb.y, offsetb.z);
}
else
{
transform.position += transform.forward * (difference * zoomRate);
}
}
}
My tracking script looks like this: public class POI : MonoBehaviour { public Transform target;
public Vector3 offset;
public float pitch = 2f;
private float currentZoom = 10f;
void LateUpdate()
{
transform.position = target.position - offset * currentZoom;
transform.LookAt(target.position + Vector3.up * pitch);
}
}
Both work as intended when separate, but not together. Please can someone help? or better yet point me in the right direction for learning how to compile both into the same script.
Thank you Joseph
Comment
Your answer
Follow this Question
Related Questions
2D moving Script 1 Answer
scripting problems please help 0 Answers
When animation end play reverse 0 Answers