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 boxing_rex · Jun 07, 2014 at 02:16 PM · c#cameraheight

Dynamic Camera Height Depending On Player / Terrain Height

Ok, So I have this script that I made, it basically follows the Player but adds some Lerping to make it smooth. But I have been having trouble making it change height when my player goes up a ramp. I would like it so the camera (Perspective) is always 12 world units above my player. Here is the camera script that I have constructed.

 public class GameCamera : MonoBehaviour {
 
     private Vector3 cameraTarget;
     private Transform target;
     
     void Start () {
     }
 
     void Update () {
         if(PlayerPrefs.GetString("Player Joined") == "True"){
             target = GameObject.FindGameObjectWithTag("Player").transform;
             cameraTarget = new Vector3(target.position.x,transform.position.y,target.position.z);
             transform.position = Vector3.Lerp(transform.position,cameraTarget,Time.deltaTime * 8);
         }
     }
 }

I cannot simply take target.position.y and add 12 because it gives me a temporary variable error. Prehaps one of you smarties can shine some light on the situation.

Thanks

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
Best Answer

Answer by tanoshimi · Jun 07, 2014 at 02:33 PM

Replace:

 cameraTarget = new Vector3(target.position.x,transform.position.y,target.position.z);

with:

 cameraTarget = new Vector3(target.position.x,target.position.y + 12f,target.position.z);
Comment
Add comment · Show 5 · 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 boxing_rex · Jun 07, 2014 at 02:38 PM 0
Share

I cant because the camera keeps zoo$$anonymous$$g out as it is in an update loop.

avatar image tanoshimi · Jun 07, 2014 at 05:37 PM 0
Share

Why would it keep zoo$$anonymous$$g out? Please check you have copied my code correctly, because it works for me.

avatar image boxing_rex · Jun 07, 2014 at 06:12 PM 0
Share

The code is copied correctly, the camera is not a child of the player if that makes any difference. The camera keeps going up and up for ever!

avatar image tanoshimi · Jun 07, 2014 at 07:03 PM 0
Share

The camera shouldn't be a child of the player. This script should be attached to the camera and the player should be tagged "Player". In the Update() loop you're then lerping the camera from it's current position to 12 units above the player's position.

The only way I can see that your camera would keep travelling upwards is if you were using transform.position.y + 12f ins$$anonymous$$d of target.position.y + 12f

avatar image boxing_rex · Jun 07, 2014 at 08:19 PM 0
Share

Thank you, that solved the problem. Guess I just didn't look at your change close enough. Thanks and sorry for any extra trouble I caused you.

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

22 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

Related Questions

Camera stay on same Y axis 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity not dividing correctly C# camera pixel width & height 1 Answer

Disable Camera Movement,Stop camera movement 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