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
1
Question by CrzypNda · May 23, 2013 at 07:46 AM · cameraterrainrtsheight

Smooth camera height adjustment based on terrain

Hello, I have written the following code for an RTS-style camera, which works "well", but not exactly how I want.

 function LateUpdate()
 {
     var currentHeight = Terrain.activeTerrain.SampleHeight(transform.position);
     transform.position.y = currentHeight + 90;
 }

Basically, it constantly detects the height of the terrain under the camera and keeps it 90 units above it. But it adjusts the height too quickly, thus if the terrain is jittery, the camera will feel jittery (e.g. it's only as smooth as the terrain is).

Below is an illustration of what I would like, but I'm not sure how to do it. Any thoughts?

Desired path of RTS camera

camerapath.png (8.7 kB)
Comment
Add comment · Show 5
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 AlucardJay · May 23, 2013 at 07:49 AM 0
Share

This has been answered many times before. Search for 1000's of answer on Lerp.

  • Lerp : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Lerp.html

  • SmoothDamp : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.SmoothDamp.html

avatar image CrzypNda · May 23, 2013 at 08:18 AM 0
Share

I have tried both of these, and neither of them actually smooths/dampens the camera path. As soon as the camera passes over terrain, it basically instantly adjusts to it.

avatar image AlucardJay · May 23, 2013 at 01:44 PM 0
Share

This seems to work fine :

 var currentHeight = Terrain.activeTerrain.SampleHeight( transform.position );
 transform.position.y = $$anonymous$$athf.Lerp( transform.position.y, currentHeight + 90.0, 0.1 );

You could change 0.1 to a lower value for a slower lerp.

avatar image CrzypNda · May 23, 2013 at 05:12 PM 0
Share

But that's not how Lerp works. According to the docs:

When t = 0 returns from. When t = 1 return to. When t = 0.5 returns the average of a and b.

t cannot be a fixed number, it needs to vary, else Lerp never actually moves the value from your starting point to your target, it just stays a certain percentage of the way through.

That's what I tried initially, and it just kept the camera at some height between my current height, and my target - it never actually animated it.

avatar image robertbu · May 23, 2013 at 05:31 PM 0
Share

Actually, the value can be fixed as long as the first parameter of the Lerp() is updated each frame. The results are an eased movement rather than a linear one. Think about it this way, with a value of .1, each frame the Lerp() will move 10% of the way from the current value to the goal value. As the distance shrinks, the 10% represents less an less distance. If the goal is moved, then the 10% will again increase.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by betaFlux · May 24, 2019 at 03:23 PM

It has been a while, but a legit question deserves an answer. May be it'll be helpful for future problem solvers. I had the same problem and just used Mathf.Lerp to set the new height.

 Vector3 camPos = transform.position;
 float groundLevel = Terrain.activeTerrain.SampleHeight(camPos);
 camPos.y = Mathf.Lerp(camPos.y, groundLevel + 90, Time.deltaTime * responsiveness); // 10 responsiveness results in a good not too lazy movement
 transform.position = camPos;
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

15 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

Related Questions

RTS camera help 1 Answer

Some questions about RTS games 1 Answer

Unity automatically rotate camera on slopes up/down 0 Answers

What options are practical for returning a character to (0,0,0)? 1 Answer

Terrain Tree Height not working on Unity 5.3.4f1 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