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
1
Question by drabs · Mar 14, 2016 at 03:27 PM · c#camerafollowsmooth

How can I get my camera to reset its position behind the player?

I am making a game where the player controls a spaceship and I wanted to add a smooth follow camera. I tried some code, but I am having some trouble. Whenever I tilt the ship down, the camera zooms out and the opposite when I tilt the ship up. Whenever I raise the position damping, the camera no longer moves smoothly with the ship. If anyone could help me with this that would be greatly appreciated. Here is my code:

using UnityEngine; using System.Collections;

public class CameraFollow : MonoBehaviour { public Transform target; // The target we are following public float distance; // The distance from the target along its Z axis public float height; // the height we want the camera to be above the target public float positionDamping; // how quickly we should get to the target position public float rotationDamping; // how quickly we should get to the target rotation

 // Use this for public variable initialization
 public void Reset() {
     distance = 1.75f;
     height = 0.5f;
     positionDamping = 10f;
     rotationDamping = 80f;
 }
 
 // LateUpdate is called once per frame
 public void LateUpdate () {
     ensureReferencesAreIntact();

     #region Get Transform Manipulation
     // The desired position
     Vector3 targetPosition = target.position + target.up * height - target.forward * distance;
     // The desired rotation
     Quaternion targetRotation = Quaternion.LookRotation(target.position-transform.position, target.up);
     #endregion
     
     #region Manipulate Transform
     transform.position = Vector3.MoveTowards(transform.position, targetPosition, positionDamping * Time.deltaTime);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationDamping * Time.deltaTime);
     #endregion
 }
 
 // Checks to make sure all required references still exist and disables the script if not
 private void ensureReferencesAreIntact() {
     if (target == null) {
         Debug.LogError("No target is set in the SmoothFollow Script attached to " + name);
         this.enabled = false;
     }
 }

}

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 Graphics_Dev · Mar 14, 2016 at 03:32 PM

Here is a clean solution for camera follow. Add a camera as a child of the player located to have the exact view you want. Disable the camera component on this object. Add another camera to your scene and add this script. Drag the other camera to it's transform property in the inspector. Hit play and watch the magic!

Use this code:

 using UnityEngine;
 using System.Collections;
 
 public class SmoothCamFollow : MonoBehaviour 
 {
     public Transform target;
     private new Transform camera;
 
     public float posSpeed = 1.0F;
     public float rotSpeed = 1.0F;
 
     // Use this for initialization
     void Start () 
     {
         camera = GetComponent<Transform> ();
     }
 
     // Update is called once per frame
     void Update () 
     {
         // position movement
         camera.position = Vector3.Lerp (camera.position, target.position, (posSpeed * Time.deltaTime));
 
         // rotation movement
         camera.rotation = Quaternion.Lerp (camera.rotation, target.rotation, (rotSpeed * Time.deltaTime));
     }
 }
 
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need help/advice with 2D Camera follow script. 1 Answer

Camera Follow sphere with ridgidbody? 4 Answers

Camera Smooth Follow 2 Answers

Having issues with a smooth camera follow 2 Answers

Smooth Camera 2D 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