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 /
  • Help Room /
avatar image
0
Question by czpeti93 · Jan 04, 2017 at 06:48 PM · camerarotationtransformlerp

How do I stop the camera from going farther away while I am moving the player and rotating the camera at the same time?

Hello, I made a simple camera controller which follows the player around from an upper perspective, I'm also using Lerp to make the camera less stiff, and I also use the Q and E keys to rotate it around the player object. I move the player using the NavMeshAgent.destination function.

The problem is that whenever I move the player and rotate the camera at the same time the camera "drifts" and the player moves further away from the camera. This effect is fixed if I remove the Lerp but I want the camera to not be so stiff but also not go further than a fixed distance away from the player, what would be the easiest way to accomplish this?

Here is the CameraController script:

 public class CameraController : MonoBehaviour
 {
     public float rotationSpeed; //this controls the speed of the rotation 
     public float damping = 1; //this adds "floatiness" to the camera
 
     private Transform player;
     private float movement;
     private Vector3 offset;
 
     void Start()
     {
         //Find the Object with the Player tag and set the initial offset
         player = GameObject.FindGameObjectWithTag("Player").transform;
         if (player == null)
         {
             Debug.LogError("Could not find GameObject with 'Player' tag");
         }
 
         offset = transform.position - player.position;
     }
 
     void LateUpdate()
     {
         Vector3 desiredPosition = player.position + offset;
         Vector3 position = Vector3.Lerp(transform.position,
             desiredPosition,
             Time.deltaTime * damping);
 
         transform.position = position;
 
         //Rotation has Q and E as Keys
         float movement = -Input.GetAxis("Rotation") * rotationSpeed * Time.deltaTime;
         if (!Mathf.Approximately(movement, 0.0f))
         {
             transform.RotateAround(player.position, Vector3.up, movement);
             offset = transform.position - player.position;
         }
 
         transform.LookAt(player);
     }
 }




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 czpeti93 · Jan 05, 2017 at 09:04 PM

I solved it using ClampMagnitude(), first I save the original offset in the distance variable, than when I rotate the camera around I clamp the magnitude of the Offset vector using the distance , I also set the Y coordinate equal to the Y coordinate of the distance vector.

EDIT: The only problem now is if I move the player towards the camera stays above the player and does not move back to the original offset...

     public float rotationSpeed; //this controls the speed of the rotation
     public float damping = 1; //this adds "floatiness" to the camera
  
     private Transform player;
     private float movement;
     private Vector3 offset;
     private Vector3 distance;
  
     void Start()
     {
         //Find the Object with the Player tag and set the initial offset
         player = GameObject.FindGameObjectWithTag("Player").transform;
         if (player == null)
         {
             Debug.LogError("Could not find GameObject with 'Player' tag");
         }
  
         offset = transform.position - player.position;
         distance = offset;
     }
  
     void LateUpdate()
     {
         Debug.Log("Distance" + distance);
         Debug.Log("Offset" + offset);
         Vector3 desiredPosition = player.position + offset;
         Vector3 position = Vector3.Slerp(transform.position,
             desiredPosition,
             Time.deltaTime * damping);
  
         transform.position = position;
  
         //Rotation has Q and E as Keys
         float movement = -Input.GetAxis("Rotation") * rotationSpeed * Time.deltaTime;
         if (!Mathf.Approximately(movement, 0.0f))
         {
             transform.RotateAround(player.position, Vector3.up, movement);
             offset = Vector3.ClampMagnitude(transform.position - player.position, distance.magnitude);
             offset.y = distance.y;
         }
  
         transform.LookAt(player);
     }

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

121 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

Related Questions

Add Rotation from parent to Quaternion Child 1 Answer

Help with rotating camera around object based on mouse input 0 Answers

Movement and Direction script bugging out 1 Answer

Smooth Camera Rotation with Deceleration 1 Answer

Creating a Window - 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