Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Herman · May 13, 2011 at 04:40 PM · cameracamera-movementlerpsmoothdampthird-person-camera

Smooth camera shift, Lerp? SmoothShift?

I have a third person camera that is targeted to my main character, I want to shift the camera left and right when I move my firing joystick accordingly, currently I am doing this by checking if my firing joystick is active, and if it is, I add my firing joystick x-position to my camera transform x-position like so:

cameraTransform.position.x += firingJoystick.position.x;

This works fine except for the fact that the camera snaps into position, and snaps back again when I release the joystick. I thought it would be an easy affair to smooth this out using SmoothDamp or Lerp, but I can't seem to figure out how to do it. As soon as I start experimenting with those things the camera shift script starts (I suspect) interfering with the rest of my camera script and the camera starts shaking, trying to be in two places at once. Does anybody mind giving me some pointers on how I can smooth out this camera shift while still just adding to the current camera position so as to avoid interfering with the rest of the script?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by e-bonneville · May 13, 2011 at 05:05 PM

I'd been trying to implement a smoothing script with Lerp, and came across the same problems you did. Turns out that the script in the docs is rather misleading. Here's why: Lerp sets a value between min and max (in this case, your camera position), and sets it to a value that reflects a decimal number between 0 and 1 that you put in. For example, say you called Lerp like this: Mathf.Lerp(0, 5, Time.time), 0, .5); You'd get 2.5 back as a result, which is .5 (50%) between 0 and 5.

Now, the problem with the documentation's example is that they're using Time.time as the interpolation value, which means that your Lerp will only work for the first second of the game, as that's the only time when Time.time is between 0 and 1, resulting in a usable decimal number for the Lerp function. (At least, that's what I recall being the problem for me)

Now, after that lengthy wall of text, here's your solution: forget Lerp. Instead, use Input.GetAxis, which returns values that are already smoothed and prettied up for you to use in your controller. You'd call it like this:

cameraTransform.position.x += Input.GetAxisHorizontal * movementSpeed;

Where movementSpeed is the speed you want to move at.

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 samf1111 · Aug 03, 2020 at 12:48 AM 0
Share

why didnt you just replace Time.time with (Time.time - delay)?

avatar image
1

Answer by yoyo · May 13, 2011 at 05:23 PM

Instead of updating the camera position immediately, you could add a local variable "Vector3 TargetPosition", and update that instead. Then lerp the camera position towards the target position each frame, something like ...

cameraTransform.position = Vector3.Lerp(cameraTransform.position, TargetPosition, 5.0f * Time.deltaTime);

Or for just the x component ...

cameraTransform.position.x = Mathf.Lerp(cameraTransform.position.x, TargetPosition.x, 5.0f * Time.deltaTime);

Note that 5 is a damping parameter -- make this larger to snap more quickly to the target position, smaller to move more slowly.

Also note that updating the camera position is usually best done in the LateUpdate method rather than Update, in your case to get the latest changes in firingJoystick, otherwise you may have the previous frame's data, which could cause jitter (though with the damping you probably wouldn't notice).

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

1 Person is following this question.

avatar image

Related Questions

ConfigurableJoint for Third-Person Camera? 0 Answers

Camera Dampening 1 Answer

Camera movement in a Third Person Shooter Game 2 Answers

Camera Rotates Incorrectly 2 Answers

2D Camera Smooth follow, FixedUpdate and LateUpdate odd difference, help needed. 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