Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by nidaunity · Jun 21, 2016 at 10:39 AM · unity5

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change the gravity of my scene? 2 Answers

UI Disappears after Ctrl+alt+del (and various other instances) [5.2.1f1] 1 Answer

When I add Baidu ads in Unity 5.4, Baidu's jar failed to load 0 Answers

Apply same transform animation to separate objects 0 Answers

Animation set duration 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges