• 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 JayFitz91 · Jul 23, 2015 at 04:20 PM · cameraplayerlerp

Camera Lerp behind player

I have a camera which follows my player around, as well rotate 360 degrees around him. The issue I'm having is the camera movement is rigid and follows directly behind the player. I would like to use a Lerp function to allow a small bit of delay to catch up to the player but I'm having trouble implementing it.

The Lerp function I have commented out works if I have none of the other stuff, but I need to get them working together, anyone have any ideas? I am using the following code:

 void Start()
     {
         //Starts the camera 5 units away
         distance = 3.5f;
 
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
     }
 
     void Update()
     {
         if (target)
         {
             x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
 
             y = ClampAngle(y, yMinLimit, yMaxLimit);
 
             Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
             Vector3 position = rotation * new Vector3(0.0f, 1.0f, -distance) + target.position;
 
             transform.rotation = rotation;
             transform.position = position;
 
             //transform.position = Vector3.Lerp(transform.position, target.transform.position + new Vector3(0.0f, 1.0f, -3.0f), Time.deltaTime * speed);
 
         }
     }
Comment
Add comment · Show 3
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 NoseKills · Jul 23, 2015 at 05:44 PM 1
Share

Does it work by replacing

 transform.rotation = rotation;
 transform.position = position;

with

 transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * speed);
 transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * speed);
avatar image JayFitz91 · Jul 23, 2015 at 05:56 PM 0
Share

@Nose$$anonymous$$ills Hi, thanks, it works now although it seems to cause a bit of jitter when my character walks, its not as smooth as it was before but the lerp functionality definitely works so thanks for that :)

avatar image JayFitz91 · Jul 23, 2015 at 06:09 PM 0
Share

@Nose$$anonymous$$ills I put it in to a FixedUpdate() and everything is smooth now, thanks for the help, if you convert your comment to an answer, I will accept it

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alexi123454 · Jul 23, 2015 at 06:12 PM

Your lerp isn't working because you're trying to lerp after you have already placed the camera at the position it's supposed to be at :P Instead of setting the position and rotation immediately, assign them to two variables called "targetPosition" and "targetRotation", and then lerp from the camera's current location and rotation to those values every frame.

Lerping towards a moving target is always going to have at least a little bit of jitter though, because lerping isn't designed to do that. Lerping is designed to smoothly go from one point to a second point. But if the second point is constantly changing, then the speed with which it moves to the second point is going to be changing every frame. It will slow down a tiny bit and then speed up a tiny bit, making for the little jittery effect.

One way to make it a little nicer would be put it in the late update function, but the final solution would be to create some actual camera movement code with acceleration and deceleration depending on where the player is.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 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

Related Questions

How you change another script value smoothly on trigger 0 Answers

Smooth camera movement 0 Answers

Moving the Camera around my Player 0 Answers

Player Camera, not main Camera. 0 Answers

Smooth camera shift, Lerp? SmoothShift? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges