Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Serge144 · Apr 12, 2020 at 09:17 PM · cameracamera-movementplayer movementcamera rotatecamera follow

Problem in rotating camera around a moving player

Ok so my Camera script is pretty simple. What the LateUpdate simply does is to keep an offset (defined on inspector) between the Camera and the Player while smoothing out the translation. This works perfectly. What i'm trying to do now is to rotate the Camera when the user wishes to (using an horizontal Joystick, the cameraJoystick) and to do so i added that code on Update. It is working, the problem with this however is that everytime i move the player away from the Camera and rotate the Camera, the Camera slowly gets aways from the Player. On the other hand, if i move the player in the direction the Camera is facing, then the Camera will slowly zoom onto the player until it gets right above the player and looking down on it. How can i fix this zoom IN/OUT behaviour?

 public class CameraFollow : MonoBehaviour
 {
     //for camera follow
     public Transform target;
     public float smoothSpeed = 0.125f;
     public Vector3 offset;
 
     //for camera rotation
     public float speed = 1.5f;
 
     public Joystick cameraJoystick;
     
     void Update(){
         if (cameraJoystick.Horizontal != 0)
         {
             transform.RotateAround(target.position, Vector3.up, -cameraJoystick.Horizontal * speed); 
             offset = transform.position - target.position;
         }
     }
 
     void LateUpdate()
     {
         follow();
     }
 
     void follow() {
         Vector3 desiredPosition = target.position + offset;
         Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
         transform.position = smoothedPosition;
         transform.LookAt(target);
     }
 }
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
1
Best Answer

Answer by Scribe · Apr 12, 2020 at 10:51 PM

The issue will like come from this code:

 offset = transform.position - target.position;

You will likely get small increase and decreases in the length of offset, because transform.position is not the same value as desiredPosition, it is somewhere inbetween as you interpolate towards desiredPosition.

Those small errors will add up over time causing the effect you see.

One solution would be to introduce another float variable, called for example followDistance. Then the calculation of offset should become:

 offset = followDistance * (transform.position - target.position).normalized;

This has the added bonus that you can introduce zooming in and out easily if you want by just increasing/decreasing the value of followDistance.

EDIT

I guess even with the changes above we are still going to accumulate errors over time as we change the value of offset at each step. Below is some code that should solve the issue, the important bit to notice is that the value of offset is never changed, so we know that when we work out the rotation it will always we some rotation of the original offset value around the up vector, which is what you want!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraFollow : MonoBehaviour
  {
      //for camera follow
      public Transform target;
      public Vector3 offset;
      public Joystick cameraJoystick;
      public float smoothSpeed = 0.1f;
      public float rotateSpeed = 90f;
 
      private float offsetRotation = 0;
      private Quaternion qRotation = Quaternion.identity;
 
      void Update(){
         if (cameraJoystick.Horizontal != 0){
             offsetRotation += -cameraJoystick.Horizontal * rotateSpeed * Time.deltaTime;
             qRotation = Quaternion.AngleAxis(offsetRotation, Vector3.up);
         }
      }
  
      void LateUpdate(){
          follow();
      }
  
     void follow() {
         Vector3 desiredPosition = target.position + (qRotation * offset);
         Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
         transform.position = smoothedPosition;
         transform.LookAt(target);
     }
  }

Comment
Add comment · Show 3 · 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 Serge144 · Apr 13, 2020 at 12:10 PM 0
Share

Thank you, yes it makes sense, i had the idea it would be that but didn't know how to fix it. I tried like this but now the behaviour is not zoom in or zoom out, it's either rotating upwards until it's right above the player, or rotating downards until the camera is touching the ground, in both cases it still faces the player.

 public class CameraFollow : $$anonymous$$onoBehaviour
 {
     //for camera follow
     public Transform target;
     public float smoothSpeed;
     public Vector3 offset;
 
     //for camera rotation
     public float speed = 1.5f;
 
     public Joystick cameraJoystick;
 
     public float followDistance;
 
     private void Start()
     {
         offset = followDistance * offset.normalized;
     }
 
     void Update(){
       if (cameraJoystick.Horizontal != 0)
       {
         transform.RotateAround(target.position, Vector3.up, -cameraJoystick.Horizontal * speed);
         offset = followDistance * (transform.position - target.position).normalized;
       }
     }
 
     void LateUpdate()
     {
         follow();
     }
 
   void follow() {
     Vector3 desiredPosition = target.position + offset;
     Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
     transform.position = smoothedPosition;
     transform.LookAt(target);
   }
 }

avatar image Scribe Serge144 · Apr 13, 2020 at 06:32 PM 0
Share

Ahh mb, should have realised that would happen. Check out my edit, the key is to not change the value of offset when working out the new position, this means we can't build up errors over time. (You may eventually want to change the value of offset, but that would be when the intent is to increase height or distance of the camera from the target, rather than when rotating around.)

avatar image Serge144 Scribe · Apr 14, 2020 at 03:52 PM 0
Share

Your edit is working like a charm :) thanks alot!

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

196 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 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 make slight Camera move and smooth 0 Answers

Camera problem 2 Answers

change objects Axis to match cameras rotation 0 Answers

URGENT Cinemachine virtual camera while falling 1 Answer

How to rotate the camera with the player smoothly without the camera snapping back and forth? 2 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