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 NitreXD · Apr 22, 2021 at 10:48 AM · scripting beginnerscriptingbasics

How do i make the camera smoothly lock to my player?

Hi. Im new to programming and i want to make a cameralock like they use in mario (When not at a certain height locked to y = 0 but when the player reaches a certain y-value the camera smoothly locks to the player.). I´ve tried a every idea i had, like adding 1 to camera y-position until it reaches player y, but nothing works. I Would really appreciate the help.

Comment
Add comment · Show 1
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 aardappel156 · Apr 22, 2021 at 01:56 PM 1
Share

https://www.youtube.com/watch?v=MFQhpwc6cKE something like this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DrJBN · Apr 23, 2021 at 07:54 PM

I don't know what mario looks like, but it sounds like you want mario to be able to move around in the FOV without being locked in the center of the screen (i.e., the camera always pointing directly at him), but have the camera move to keep him there when he gets close to an edge? I have a couple of space games (3d) where I do the same thing. Keep a direction to the object (mario). Use the camera WorldtoScreen function to get the screen coordinates of mario in the range of 0-1. Get the absolute value of the distance between mario and the screen coordinates of the allowable Y extent, and put that in the range of zero to 1. (y = Mathf.abs(mario.screeny-.5)/0.5. Now, what you can do is lerp the camera's forward vector toward mario as a function of the number you just calculated. lerpspeed = MathF.Pow( y,somevalue); 3 works good for me as somevalue. What that last part does, the exponentiating, is it makes the value of lerpspeed grow very slowly. If mario is at the edge of your allowable Y, then the value of y you calculate first will be very close to 1, and when you exponentiate it, you'll get something very close to 1 (1^X = 1) and your camera will follow Mario precisely. As he moves away from that allowable Y value, the y you calculate will be smaller (e.g., .25), and when you exponentiate it it will get even smaller (,.0156) making your camera hardly move at all.

Edit: you can do the same thing in the world coordinates, just get the distance between mario and your Y barrier and put it into the range of 0-1, then exponentiate it, and use that as the lerpspeed and lerp your camera.transform.forward towards mario using that lerpspeed.

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

131 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

Related Questions

How to implement tokenization of game characters? 1 Answer

Do all scripts have to be attached to an object? 1 Answer

Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers

Getting a " NullReferenceException: Object reference not set to an instance of an object " - Space Shooter Tutorial 14 - Counting Points 3 Answers

weighted inventory system 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