Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by PoTa_Steve · Aug 03, 2020 at 08:19 AM · camera-movementsmoothing

How do I make a smooth animation in unity

I am trying to make a map in unity in which you can zoom on the player when you press the F key. For now, I just made a simple code that allows you to move the map with WASD or arrows keys you can zoom in and out with the mouse scroll wheel and by pressing F the camera focuses on the position of the player but the animation is instantaneous. What do I have to do to move the camera progressively? Here it is the code...

 public class BigMapMov : MonoBehaviour
 {
     public Camera bigMapCam;
     public float moveSpeed;
     public GameObject player;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         MoveCam();
         
         if (Input.GetAxis("Mouse ScrollWheel") < 0f) // diminuisce
         {
             bigMapCam.orthographicSize += 50;
         }
         else if (Input.GetAxis("Mouse ScrollWheel") > 0f) // ingrandisce
         {
             bigMapCam.orthographicSize -= 50;
         }
 
         if (bigMapCam.orthographicSize > 3500)
         {
             bigMapCam.orthographicSize = 3500;
         }
         if (bigMapCam.orthographicSize < 50)
         {
             bigMapCam.orthographicSize = 50;
         }
     }
 
     void MoveCam()
     {
         //Focus on the player
         if (Input.GetKeyDown(KeyCode.F))
         {
             FocusOnPlayer();
         }
 
         //Get the input
         float camX = Input.GetAxisRaw("Horizontal");
         float camZ = Input.GetAxisRaw("Vertical");
 
         //speed fix
         if (3500 <= bigMapCam.orthographicSize && bigMapCam.orthographicSize > 2400)
         {
             moveSpeed = 2f;
         }
         else if (2400 <= bigMapCam.orthographicSize && bigMapCam.orthographicSize > 1200)
         {
             moveSpeed = 4f;
         }
         else if (1200 <= bigMapCam.orthographicSize && bigMapCam.orthographicSize > 50)
         {
             moveSpeed = 8f;
         }
 
         //camera movement
         if (bigMapCam.orthographicSize <= bigMapCam.transform.position.x && (7000 - bigMapCam.orthographicSize) >= bigMapCam.transform.position.x)
         {
             bigMapCam.transform.position += new Vector3(camX * moveSpeed, 0, 0);
         }
         if (bigMapCam.orthographicSize <= bigMapCam.transform.position.z && (7000 - bigMapCam.orthographicSize) >= bigMapCam.transform.position.z)
         {
             bigMapCam.transform.position += new Vector3(0, 0, camZ * moveSpeed);
         }
 
 
         if (bigMapCam.transform.position.x > (7000 - bigMapCam.orthographicSize))
         {
             if ((bigMapCam.transform.position.x - (7000 - bigMapCam.orthographicSize)) >= 10)
             {
                 bigMapCam.transform.position += new Vector3(-15, 0, 0);
             }
             else
             {
                 bigMapCam.transform.position += new Vector3(-1, 0, 0);
             }
         }
 
         if (bigMapCam.transform.position.x < (bigMapCam.orthographicSize))
         {
             if ((bigMapCam.transform.position.x - bigMapCam.orthographicSize) <= -10)
             {
                 bigMapCam.transform.position += new Vector3(15, 0, 0);
             }
             else
             {
                 bigMapCam.transform.position += new Vector3(1, 0, 0);
             }
         }
 
         if (bigMapCam.transform.position.z > (7000 - bigMapCam.orthographicSize))
         {
             if ((bigMapCam.transform.position.z - (7000 - bigMapCam.orthographicSize)) >= 10)
             {
                 bigMapCam.transform.position += new Vector3(0, 0, -15);
             }
             else
             {
                 bigMapCam.transform.position += new Vector3(0, 0, -1);
             }
         }
 
         if (bigMapCam.transform.position.z < (bigMapCam.orthographicSize))
         {
             if ((bigMapCam.transform.position.z - bigMapCam.orthographicSize) <= -10)
             {
                 bigMapCam.transform.position += new Vector3(0, 0, 15);
             }
             else
             {
                 bigMapCam.transform.position += new Vector3(0, 0, 1);
             }
         }
     }
 
     void FocusOnPlayer()
     {
         if (player.transform.position.x <= 50)
         {
             bigMapCam.orthographicSize = 50;
 
             bigMapCam.transform.position += new Vector3((50 - bigMapCam.transform.position.x), 0, 0);
         }
         else if (player.transform.position.x > 50 && player.transform.position.x < 6950)
         {
             bigMapCam.orthographicSize = 50;
 
             bigMapCam.transform.position += new Vector3((player.transform.position.x - bigMapCam.transform.position.x), 0, 0);
         }
         else if (player.transform.position.x >= 6950)
         {
             bigMapCam.orthographicSize = 50;
 
             bigMapCam.transform.position += new Vector3((6950 - bigMapCam.transform.position.x), 0, 0);
         }
 
         if (player.transform.position.z <= 50)
         {
             bigMapCam.orthographicSize = 50;
 
             bigMapCam.transform.position += new Vector3(0, 0, (50 - bigMapCam.transform.position.z));
         }
         else if (player.transform.position.z > 50 && player.transform.position.z < 6950)
         {
             bigMapCam.orthographicSize = 50;
 
             bigMapCam.transform.position += new Vector3(0, 0, (player.transform.position.z - bigMapCam.transform.position.z));
         }
         else if (player.transform.position.z >= 6950)
         {
             bigMapCam.orthographicSize = 50;
 
             bigMapCam.transform.position += new Vector3(0, 0, (6950 - bigMapCam.transform.position.z));
         }
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mrintoxx · Aug 03, 2020 at 10:48 AM

Maybe check Vector3.Lerp to smooth the movement of the camera. you have the doc here

replace :

 bigMapCam.transform.position += new Vector3((50 - bigMapCam.transform.position.x), 0, 0);

by :

 bigMapCam.transform.position = Vector3.Lerp(startPos, endPos, timeTravel);
Comment
Add comment · Share
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

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

140 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble with logic related to my camera smoothing 0 Answers

Multipurpose CameraRig - Smooth Movement - how to achieve? 1 Answer

Camera Smoothing from pos A - pos B in Unity 2D 0 Answers

Adjust camera orientation through code 0 Answers

How to move camera so that the finger points to the same spot? 1 Answer


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