third person camera zoom in and zoom out
hello, i m creating third person shooting game, but unable to create camera zoom in and zoom out. here is my script to follow player public class AimCrosshair : MonoBehaviour { public Transform CameraTarget; public Transform scopeZoom; public Transform startPos; Vector3 pos; private float x = 0.0f; private float y = 0.0f; public float MaxViewDistance = 0.7f; public float MinViewDistance = 0.01f; public int ZoomRate = 20; private int lerpRate = 5; private float correctedDistance; private float currentDistance; public static float targetRotantionAngle; public static float targetRotantionAngle1; float cameraRotationAngle; float cameraRotationAngle1; Quaternion rotation; Vector3 position; bool isCorrected; public float cameraTargetHeight = 0.01f; float distance; // Use this for initialization void Start () { Vector3 Angles = transform.eulerAngles; x = Angles.x; y = Angles.y; }
void LateUpdate () {
float distance = 3f;
rotate.get.target.transform.localEulerAngles = new Vector3 (0, Camera.main.transform.localEulerAngles.y, 0);
targetRotantionAngle = CameraTarget.eulerAngles.y;
cameraRotationAngle = transform.eulerAngles.y;
targetRotantionAngle1 = CameraTarget.eulerAngles.x;
cameraRotationAngle1 = transform.eulerAngles.x;
x = Mathf.LerpAngle (cameraRotationAngle, targetRotantionAngle, lerpRate * Time.deltaTime);
y = Mathf.LerpAngle (cameraRotationAngle1, targetRotantionAngle1, lerpRate * Time.deltaTime);
// y = ClampAngle (y, -15, 25);
rotation = Quaternion.Euler (y, x, 0);
// position = Vector3.Lerp (Camera.main.transform.position, scopeZoom.transform.position, Time.deltaTime 5); // transform.position = position; // transform.rotation = rotation; distance = Mathf.Clamp (distance, MinViewDistance, MaxViewDistance); correctedDistance = distance; position = CameraTarget.position - (rotation Vector3.forward * distance); isCorrected = false;
// currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * ZoomRate) : correctedDistance;
//position = CameraTarget.position - (rotation * Vector3.forward * currentDistance + new Vector3 (0, -cameraTargetHeight, 0));
position = Vector3.Lerp (scopeZoom.transform.position, startPos.transform.position, Time.deltaTime * 5);
transform.rotation = rotation;
transform.position = position;
if(Input.GetKeyDown(KeyCode.Space)){
rotate.get.target.transform.localEulerAngles = new Vector3 (0, Camera.main.transform.localEulerAngles.y, 0);
distance = 1.5f;
targetRotantionAngle = CameraTarget.eulerAngles.y;
cameraRotationAngle = transform.eulerAngles.y;
targetRotantionAngle1 = CameraTarget.eulerAngles.x;
cameraRotationAngle1 = transform.eulerAngles.x;
x = Mathf.LerpAngle (cameraRotationAngle, targetRotantionAngle, lerpRate * Time.deltaTime);
y = Mathf.LerpAngle (cameraRotationAngle1, targetRotantionAngle1, lerpRate * Time.deltaTime);
// y = ClampAngle (y, -15, 25);
rotation = Quaternion.Euler (y, x, 0);
// MaxViewDistance = 0.6f; // distance = Mathf.Clamp (distance, MinViewDistance, MaxViewDistance); // correctedDistance = distance; // position = CameraTarget.position - (rotation Vector3.forward distance); // isCorrected = false; //
// currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime ZoomRate) : correctedDistance; // position = CameraTarget.position - (rotation Vector3.forward * currentDistance + new Vector3 (0, -cameraTargetHeight, 0)); position=Vector3.Lerp(transform.position,scopeZoom.transform.position,Time.deltaTime*5f); transform.rotation = rotation; transform.position = position; Camera.main.fieldOfView = 25f; } if(Input.GetKeyUp(KeyCode.Space)){ Camera.main.fieldOfView=60f; MaxViewDistance=0.7f; distance = 3f; } } private static float ClampAngle(float angle, float min, float max) { if (angle < -360) { angle += 360;
} if (angle > 360) { angle -= 360;
} return Mathf.Clamp (angle,min,max); } which is applied on camera. and there is one more script on camera to rotate with mouse public Transform target; public float speed=3.0f; public static rotate get; // Use this for initialization void Start () { get = this; }
// Update is called once per frame
void Update () {
transform.LookAt (target);
transform.RotateAround (target.position, Vector3.up, Input.GetAxis ("Mouse X") * speed);
//transform.RotateAround (target.position,Vector3.right, Input.GetAxis ("Mouse Y") * speed);
}
on the beginning follow script is off and when player start walking then follow script is on. how i make camera zoom in and zoomOut .plz help