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 Alan Wyvern · Apr 11, 2016 at 08:10 AM · c#smoothcamera movement

Smooth Camera Zoom on movement not working

I'm trying to get my camera to zoom depending on how fast the player is moving. So the faster you go, the further out the camera zooms (orthagonal size increases) so you can see what's ahead. On acceleration and normal deceleration it works fine, but when you crash into something (And velocity gets to near zero instantly) the camera jerks down to minimum distance in one frame, which isn't smooth at all. Not sure where I should go from here, here's the code I'm using at the moment:

 public class CameraMovement : MonoBehaviour {


 public Transform ship;
 public Camera mainCamera;

 public float xBound = 6.0f;
 public float yBound = 3.0f;
 public float camHeight = 10.0f;
 public float camMaxHeight = 10.0f;
 public float dampMove = 2.0f;
 public float speedAdjuster = 1.0f;

 private Rigidbody2D shipRB;

 private float xPos;
 private float yPos;
 private float zPos;

 private float oldPosition = 0;
 private float newPosition = 0;

 void Start ()
 {
     shipRB = ship.GetComponent<Rigidbody2D>();
 }

 void Update ()
 {
     xPos = ship.position.x + Mathf.Clamp(shipRB.velocity.x / dampMove, -xBound, xBound);
     yPos = ship.position.y + Mathf.Clamp(shipRB.velocity.y / dampMove, -yBound, yBound);
     zPos = -10;

     oldPosition = newPosition;
     newPosition = Mathf.Clamp(shipRB.velocity.magnitude / dampMove, 0.0f, (camMaxHeight - camHeight)); 

     mainCamera.orthographicSize = camHeight + Mathf.Lerp(oldPosition, newPosition, Time.deltaTime*speedAdjuster);


     transform.position = new Vector3 (xPos, yPos, zPos);

 }

}

What do I need to do/change to enable a smoother camera zoom (orthagonal size movement)?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by b1gry4n · Apr 11, 2016 at 08:39 AM

You are lerping the orthographic size, but you arent lerping the cameras position.

 transform.position = Vector3.Lerp(transform.position, new Vector3(xPos, yPos, zPos), Time.deltaTime * speedAdjuster);
Comment
Add comment · Show 1 · 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
avatar image Alan Wyvern · Apr 11, 2016 at 09:15 PM 0
Share

I don't need to lerp the position as it's working as intended. It's just the zoom I need to lerp which is controlled by the orthogonal size, but it ain't working!

avatar image
0

Answer by Redwolve · Apr 12, 2016 at 03:16 AM

@Alan Wyvern

Could you set a conditional up that has a default setting anytime velocity is less than a certain value? like say you have the same view distance for 0-2 velocity units, and then you use your algorithm for the rest of the speeds?

If that still causes too much that isn't smooth you could have crashes stop your normal controls and run a co-routine that takes you from the current speed to the zero speed and back to the players speed until back to normal. if i am writing that in a way that makes sense. Could have a Steady() and a Crash() and have the Steady() Stopped when a crash is detected and then start the Crash() and then vice versa to come out of it.

Those are the two ideas I came up with. Let me know if you continue to find difficulties and I will take a bit more time with it and see what I can do.

Hope this helps, let me know!

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

Answer by Alan Wyvern · Apr 24, 2016 at 02:12 PM

Got to the bottom of this one, I was updating the from position on the lerp when really I shouldn't have done that. Here's the working code:

 void Update()
 {
     Vector3 offsetTarget = new Vector3(Mathf.Clamp(shipRB.velocity.x / dampMove, -xBound, xBound),
                                        Mathf.Clamp(shipRB.velocity.y / dampMove, -yBound, yBound),
                                        0.0f);
     
     float distanceTarget = Mathf.Clamp(shipRB.velocity.magnitude / dampMove, 0.0f, (camMaxHeight - camHeight));

     offset = Vector3.Lerp(offset, offsetTarget, Time.deltaTime * speedAdjuster);
     distance = Mathf.Lerp(distance, distanceTarget, Time.deltaTime * speedAdjuster);

     mainCamera.orthographicSize = camHeight + distance;

     transform.position = new Vector3(ship.position.x, ship.position.y, -10) + offset;
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

3d camera not working as I want it to 0 Answers

How to stop the camera from clipping through mesh/walls/objects? 3 Answers

Unity custom Audio Manager won't work with custom Stop function 1 Answer

Mega Man Style Camera Movement 0 Answers

Click Drag Camera Orbit around a static Target 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