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
0
Question by siddharth3322 · Feb 03, 2015 at 03:16 PM · cameraunity 2dcamera-movementcamera follow

Follow camera in 2d game

Recently I am working on racing game. For that I am following camera with car. But using following code it creates jerk in movement.

I don't want any rotation, I just want to follow in y direction.

 public class SmoothCameraFollow : MonoBehaviour
 {
 
     private Vector3 cameraOffset, playerOffset;
     public float speed;
     public Transform target;
 
     void Start ()
     {
         cameraOffset = transform.position;
         playerOffset = target.position;
     }
     
     void LateUpdate ()
     {
         if (GameManager.Instance.IsCarReady) {
             Vector3 targetPosition = target.position + cameraOffset - playerOffset;
             targetPosition.x = 0;
             transform.position = targetPosition;
         }
     }
 }

Please give some help in this.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Imtiaj Ahmed · Feb 03, 2015 at 03:27 PM

Try this-

     Vector3 refVelocity = Vector3.zero;
     float smoothTime = 0.5f;
     .....
 
     void Update () {
         cameraOffset = transform.position;
     }
     void LateUpdate(){
     //Your codes for targetPosition here and then,
         transform.position = Vector3.SmoothDamp (cameraOffset, targetPosition, ref refVelocity, smoothTime);
     }

It will smoothly follow your car. This is what I used once.

Comment
Add comment · Show 6 · 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 siddharth3322 · Feb 03, 2015 at 03:28 PM 0
Share

Okay, I will test this and reply to you.

avatar image siddharth3322 · Feb 03, 2015 at 03:32 PM 0
Share

What is meaning of ref refVelocity because at initialization time you have set it only one? At present camera is not moving from its place. What next to do?

avatar image Imtiaj Ahmed · Feb 03, 2015 at 03:39 PM 0
Share

http://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

Edited my answer. Get the camera position in Update()

avatar image siddharth3322 · Feb 04, 2015 at 06:07 AM 0
Share

No your modified code also not generating smooth result for me.

At present only above code generate good result for me. No Lerp() or Damp() function helping me. But I am expecting more smooth result compare to this. So I am here.

avatar image Imtiaj Ahmed · Feb 04, 2015 at 08:47 AM 0
Share

Well, sorry to hear that. There are a lot of things that may cause jerkiness and there are a lot of discussion threads out there. If you are using pro, you can find the spikes in the profiler that may cause you the jerk.

Show more comments
avatar image
0

Answer by VOTRUBEC · Feb 04, 2015 at 10:08 AM

This code is working correctly for me:

 public class CameraScript : MonoBehaviour
 {
 
     // Set Player in the Editor.
     public GameObject Player;

     // Set CamerOffset in the Editor.
     public Vector2 CamerOffset;
 
     Vector3 cameraPosition;
 
     // Use this for initialization
     void Start ( )
     {
         cameraPosition = new Vector3 ( 0, 0, -10.0f );
     }
 
     void Update ( )
     {
         cameraPosition.x = Player.transform.position.x + CamerOffset.x;
         cameraPosition.y = Player.transform.position.y + CamerOffset.y;
         this.transform.position = cameraPosition;
     }
     
 }
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

21 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

Related Questions

Unity 2D Fighting Game Camera 0 Answers

I can not find Cinemachine in package manager. There is no option of 'All Assets in package manager.' 1 Answer

Can you offset the dead zone of Cinemachine's camera from the center? 0 Answers

Make a camera to Follow two players 2 Answers

How can camera smoothly follow a jerky game object? (bouncing up and down) 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