• 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 · Mar 31, 2015 at 11:27 AM · camera-movementfollow playersmoothfollow

Follow Player in 2D Game

At present I am working on 2d side scrolling game. I want camera follow for player movement only in horizontal movement. Right now I have written this type of target following script

 public class SmoothFollow : MonoBehaviour
 {
     public Transform target;
     public float smoothness = 10f;
     private Vector3 targetInitialPos;
 
     void OnEnable ()
     {
         target = null;
     }
 
     void Update ()
     {
         if (!target) {
             target = GameObject.Find ("MrJump").transform;
             targetInitialPos = target.position;
         } else {
 
             Vector3 cameraPosition = target.position - targetInitialPos;
             cameraPosition.y = 0f;
             cameraPosition.z = -10f;
 //            transform.position = cameraPosition;
             transform.position = Vector3.Lerp (transform.position, cameraPosition, Time.deltaTime * smoothness);
 
         }
     }
 }

If I directly apply cameraPosition to camera transform then it stuttering in mobile devices. So that I used lerp for this purpose but didn't got success in that also. I have attached small video also to represent my problem correctly.

Camera Stuttering Video

Using above code it shows jerky movement of camera. Not smoother one that I want. Please give some suggestion in this.

Comment
Add comment · Show 3
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 taxvi · Mar 31, 2015 at 12:48 PM 0
Share

mm... and why did you comment out the 20th line ins$$anonymous$$d of 18th?

avatar image siddharth3322 · Mar 31, 2015 at 12:49 PM 0
Share

Because doesn't notice any changes in output. Still noticing jerky camera movement.

avatar image siddharth3322 · Apr 01, 2015 at 03:01 PM 0
Share

Please give some help.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Surprisejedi · Mar 31, 2015 at 10:28 PM

Ahhh, I had this problem too! But I found the solution but very sadly it was in 3d. But I may be able to help you. First make sure that the smooth follow target is the player. Second make sure the distance is 0 and that's what helped me! Hope it helped you!

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
avatar image
0

Answer by Surprisejedi · Mar 31, 2015 at 10:28 PM

And if that doesn't help than add mouse orbit. If your game is based on going upwards make sure that Max and Min x is 0 or if it is horizontal than make Max and Min y 0. Hope all my advice helped! :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Smooth Camera follow for 2D platformer game 1 Answer

how can i avoid the rotation effect of the camera being the child of the player? #c 2 Answers

Camera not following player.,Camera not following player... 1 Answer

How to make an camera offsets in Unity Smooth Follow Standard Assets? 0 Answers

camera problem 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges