Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 epicman245 · Jul 04, 2014 at 03:33 AM · c#camera

C# - How to smoothly & slightly move camera while player moves?

Okay, it may sound confusing, but what I want to do is basically when the player starts moving I want the camera to move slightly to catch up. Instead of following it statically, it sort of catches up to it. BTW it is a 2D game

Example: The player moves over to the right, the camera moves over about a fraction after so it seems like the camera is catching up to the player. I don't know how to explain it much further than that. Sorry.

Thanks in advance!

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 Wiki

Answer by seandanger · Jul 04, 2014 at 05:54 AM

Unity has an example script for this, called SmoothFollow2D.js, and it's part of the Standard Assets(Mobile) asset package included with Unity.

To load it up, go to the Unity Menu > Assets > Import Package > and select Standard Assets (Mobile). Then, under the Control Setups folder, load the SidescrollSetup.unity scene file.

Now find the Main Camera GameObject in the Hierarchy. With that object selected, you will see the script attached as a component under the Inspector window.

Just in case you or others find it necessary, here's the script in C# format:

 public class CSharpSmoothFollow2D : MonoBehaviour 
 {
     public Transform target;
     public float smoothTime = 0.3f;
     private Transform thisTransform;
     private Vector2 velocity;
 
     private void Start()
     {
         thisTransform = transform;
     }
 
     private void Update() 
     {
         Vector3 vec = thisTransform.position;
         vec.x = Mathf.SmoothDamp( thisTransform.position.x, 
             target.position.x, ref velocity.x, smoothTime);
         vec.y = Mathf.SmoothDamp( thisTransform.position.y, 
             target.position.y, ref velocity.y, smoothTime);
         thisTransform.position = vec;
     }
 }



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 epicman245 · Jul 04, 2014 at 08:33 AM 0
Share

Thanks! But I'm trying to do a top-down game and I don't want to follow it by the Y. but the Z I changed the code but I get an error saying Vector2 doesn't have 'z'

Could there be an alternate with the Z coordinate?

EDIT: I fixed it by changing Vector2 velocity to a Vector3

avatar image mifzer · Dec 14, 2015 at 08:50 AM 0
Share

thanks full :D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Functions via camera rotations... 1 Answer

No Glitch Please ... 1 Answer

C# FP_Camera not following Character Controller 0 Answers

C# locking Main Camera's Rotations 1 Answer

capture image on phone and use it as background of the scene 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