Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 aly.lear · Mar 12, 2016 at 02:34 PM · cameracamera-movementcamera follow

Does anyone know the script for a smooth camera follow of the main game object?

I've been researching this for about two weeks and have come across nothing that works. Anything will be helpful!

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

8 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Lanthuas · Nov 20, 2018 at 08:28 AM

This works really nice for me. You can also find it on official unity documentation page.

  // Smooth towards the target
     
     using UnityEngine;
     using System.Collections;
     
     public class DampCamera2D : MonoBehaviour
     {
         public Transform target;
         public float smoothTime = 0.3F;
         private Vector3 velocity = Vector3.zero;
     
         void Update()
         {
             // Define a target position above and behind the target transform
             Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10));
     
             // Smoothly move the camera towards that target position
             transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
         }
     }
 


Comment
Add comment · Show 2 · 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 ShamusO · Apr 12, 2020 at 08:36 PM 0
Share

This doesn't work at all for me the character just runs away from the camera, though the camera does mirror the player rotation.

avatar image UnityToMakeMoney · Dec 19, 2020 at 09:05 PM 0
Share

@ShamusO You just need to change this line of code: Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10)); Change the 5 to a 0-2. This line of code deter$$anonymous$$es how far the camera should be at all times from the target. Here the camera is always 5 points above the target at all times.

avatar image
3

Answer by rocker182 · Jun 18, 2019 at 08:52 PM

I usually go with something i can tweak in editor i set the right position in editor and voilla this helps lerp camera in place nicely:

     public Transform target;
     public Vector3 target_Offset;
 private void Start()
 {
     target_Offset = transform.position - target.position;
 }
 void Update()
 {
     if (target)
     {
         transform.position = Vector3.Lerp(transform.position, target.position+target_Offset, 0.1f);
     }
 }`


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 ShamusO · Apr 12, 2020 at 08:32 PM 0
Share

For some reason, $$anonymous$$y camera shakes a bit with this sort of setup. Like it is stuttering when the object gets to its "$$anonymous$$ax distance". Do you know why that is?

avatar image N-8-D-e-v ShamusO · Oct 03, 2020 at 09:06 PM 0
Share

try smoothdamp

avatar image aakashsolanki890 · Nov 14, 2021 at 04:24 PM 0
Share

you can find smooth camera follow script here:-

https://gamedevsolutions.com/?p=79

hope it was helpful.

avatar image
1

Answer by Zoogyburger · Mar 12, 2016 at 07:47 PM

Here's a real simple follow camera script:

     public Transform target;
 
     void Update()
     {
         if (target)
         {
             transform.position = Vector3.Lerp (transform.position, target.position, 0.1f);
         }
     }
 }

Just put the script on the Camera and in the Inspector set the target to be the main gameobject.

Comment
Add comment · Show 2 · 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 aly.lear · Mar 13, 2016 at 10:28 PM 0
Share

Unfortunately, that didn't work. I tried different things with it and it didn't work.

avatar image Zoogyburger · Mar 13, 2016 at 10:39 PM 1
Share

How did it not work? $$anonymous$$ake sure the Target is Set to be the Player.

$$anonymous$$aybe this will work:

 public Transform target;
 
     void Update()
     {
         if (target)
         {
             transform.position = Vector3.Lerp (transform.position, target.position, 0.1f)+ new Vector3 (0, 0, -10);
         }
     }
 }
avatar image
0

Answer by hexagonius · Mar 12, 2016 at 04:27 PM

install the standard assets with Unity. there's a follow script packed along with it

Comment
Add comment · Show 1 · 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 aly.lear · Mar 12, 2016 at 07:29 PM 0
Share

Where can I do that?

avatar image
0

Answer by kskjadav007 · Feb 21, 2018 at 12:24 PM

maybe this will help

link text

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
  • 1
  • 2
  • ›

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

12 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

Related Questions

Camera follow issue. 2 Answers

Camera EdgeScrolling Question 0 Answers

Scripting beginner - how can I make the camera follow the character but still face the same direction? 2 Answers

Help with setting up 2D auto scrolling camera using Cinemachine 1 Answer

HTC Vive camera deformed when assigned a parent 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