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 Tap1 · Sep 22, 2017 at 08:55 PM · camerarotationfollow playersmoothfollow

How I can make camera rotate smoothly followed object?

I have a game where is camera top to down and it is driving game. Now I need that camera follow car smoothly not just parenting. This is my code that works with the position. Problem is when the car turns 45 degrees or 90 degrees and then the screen is not turning. So I need to know how I can make the camera rotate smoothly same direction than the car. But camera should rotate only in the x-axis. The y-axis is always 90 degrees and the z-axis is 0 degrees.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SmoothFollow : MonoBehaviour {
 
     public float interpVelocity;
     public float minDistance;
     public float followDistance;
     public GameObject target;
     public Vector3 offset;
     Vector3 targetPos;
 
     // Use this for initialization
     void Start()
     {
         targetPos = transform.position;
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         if (target)
         {
             Vector3 posNoZ = transform.position;
             posNoZ.z = target.transform.position.z;
 
             Vector3 targetDirectionZ = (target.transform.position - posNoZ);
 
             interpVelocity = targetDirectionZ.magnitude * 5f;
 
             targetPos = transform.position + (targetDirectionZ.normalized * interpVelocity * Time.deltaTime);
 
             transform.position = Vector3.Lerp(transform.position, targetPos + offset, 0.25f);
 
 
             Vector3 posNoX = transform.position;
             posNoX.x = target.transform.position.x;
 
             Vector3 targetDirectionX = (target.transform.position - posNoX);
 
             interpVelocity = targetDirectionX.magnitude * 5f;
 
             targetPos = transform.position + (targetDirectionX.normalized * interpVelocity * Time.deltaTime);
 
             transform.position = Vector3.Lerp(transform.position, targetPos + offset, 0.25f);
 
 
             
         }
     }
 }
 


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 frankslater · Sep 23, 2017 at 11:25 AM 0
Share

You mentioned the camera is top down. That would mean you are rotating it around the y axis. In Unity terms y is the vertical axis. If you are planning to use physics as well, you might be better of adjusting to Unity, or I can help you adjusting my code. In the answer I will need to keep it correct for the default Unity world space in case others are looking for the same answer.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by frankslater · Sep 23, 2017 at 11:06 AM

I see others posted as well by the time I setup the environment and wrote the code, but give this a try:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SmoothFollow : MonoBehaviour {
     public Transform target;
     public float followSpeed = 3f;
     public float rotationSpeed = 3f;
 
     float distance;
     Vector3 position;
     Vector3 newPos;
     Quaternion rotation;
     Quaternion newRot;
 
 
     // Use this for initialization
     void Start() {
         distance = transform.position.y - target.position.y;
         position = new Vector3(target.position.x, target.position.y + distance, target.position.z);
         rotation = Quaternion.Euler(new Vector3(90, target.rotation.eulerAngles.y, 0f));
     }
     
     void FixedUpdate() {
         if (target) {
             newPos = target.position;
             newPos.y += distance;
             newRot = Quaternion.Euler(new Vector3(90f, target.rotation.eulerAngles.y, 0f));
             position = Vector3.Lerp(position, newPos, followSpeed * Time.deltaTime);
             rotation = Quaternion.Lerp(rotation, newRot, rotationSpeed * Time.deltaTime);
             transform.position = position;
             transform.rotation = rotation;
         }
     }
 }

The up and down direction might not be the same axis for you. In that case just change the axis, or let me know what your up direction is and I'll change it for you.

The position and rotation variables are to save on the performance, so you are not accessing transform.position and transform.rotation that many times.

UPDATE: Thanks @Tap1 for accepting the answer. Let me know how you get on with shakiness. I did write a quick and dirty target mover script I tested it with on the morning (moved a cube object around) and it seemed smooth to me. Here is that code if it helps:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CarMover : MonoBehaviour {
     public float speed = 30f;
     public float rotationSpeed = 60f;
 
     Vector3 rotationEuler;
     float turn;
 
     void Start() {
         rotationEuler = transform.rotation.eulerAngles;
     }
 
     void FixedUpdate() {
         float inputHorizontal = Input.GetAxis("Horizontal");
         float inputVertical = Input.GetAxis("Vertical");
 
         rotationEuler.y += inputHorizontal * rotationSpeed * Time.deltaTime;
 
         transform.rotation = Quaternion.Euler(rotationEuler);
         transform.localPosition += transform.forward * inputVertical * speed * Time.deltaTime;
     }
 }

Also made a video video about running it in Unity.

Comment
Add comment · Show 5 · 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 Tap1 · Sep 23, 2017 at 01:04 PM 0
Share

THIS WOR$$anonymous$$!!! Thank you so much!

avatar image Tap1 · Sep 23, 2017 at 02:04 PM 0
Share

Wait this is now shaky. How I can smoothly move camera with mathf.lerp in this code. Can you make smoother code?

avatar image frankslater Tap1 · Sep 23, 2017 at 03:27 PM 0
Share

By shaky, do you mean jerky? If so, do both your camera and target are updated in FixedUpdate()?

Could better help if I could see the issue. I built a quick test environment before I posted the code but that could be very different from what you are trying to do. Do you $$anonymous$$d sending me the code that updates your target?

avatar image frankslater frankslater · Sep 24, 2017 at 10:46 AM 0
Share

It's been a while since I've done this and I didn't use Time.deltaTime to the calculations (which is recommended to be used "because it automatically returns the right delta time if you are inside a FixedUpdate"). Updated the scripts in the answer. That wouldn't cause shakiness on its own thought. What is your Fixed timestep value (Edit > Project Settings > Time)?

Show more comments
avatar image
0

Answer by Creeper_Math · Sep 22, 2017 at 09:03 PM

Could try adding in this after your movement script

 Vector3 targetRotation = target.transform.rotation.eulerAngles;
 
 Vector3 cameraRotation = transform.rotation.eulerAngles;
 
 transform.rotation.Euler(Vector3.Lerp(cameraRotation,targetRotation));
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 Tap1 · Sep 23, 2017 at 09:25 AM 0
Share

I added this to my code but it underlines it and print error "$$anonymous$$ember ' Quaternion.Euler (Vector3)' cannot be accessed with an instance reference qualify it with a type name ins$$anonymous$$d" What this mean? And also Lerp need 3 parameters but I added Time.deltaTime and I think that work.

avatar image tmalhassan Tap1 · Sep 23, 2017 at 09:28 AM 0
Share

You get the error on the last line?

avatar image Tap1 tmalhassan · Sep 23, 2017 at 10:27 AM 0
Share

Yes, but how I can fix it?

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

127 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

Related Questions

How to make the camera NOT rotate with it's father 1 Answer

Clamping Between Two Different Ranges 1 Answer

Unity3D accelerometer camera rotation realistic controls 0 Answers

Set the rotation axis of the camera 0 Answers

Add Rotation from parent to Quaternion Child 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