• 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 Sarseth · Jan 13, 2015 at 09:27 PM · camera2dfollow

Camera 2d following player with delay jumps

Hey, I was looking for answer for two days. Could not find it, or it just did not work as expected.

I need in my 2D game (X,Y) a camera that follow player. But the thing I need is that camera has some delays. Player starts moving and camera's waiting like 0.5 second (customizable) and starts follow in normal speed. + Player can run, then this delay should be a little bigger. And when player stops, camera normalize its position.

Well, I have this effect but.. while player walks, player is jumping likehow. You see something like little fps problem but it's not the issue. I've menage to find, that the problem is time parameter. But without time I have no idea how to make it. Using Vector3.Lerp, SmoothDamp, Slerp (don't know difference yet between them) and others.

 method1(){
     Vector3 point = camera.WorldToViewportPoint (playerTransform.position);
     Vector3 delta = playerTransform.position - camera.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
     Vector3 destination = transform.position + delta;
     transform.position = Vector3.SmoothDamp (transform.position, destination, ref velocity, dampTime);
 }
 method2(){
      float newX = target.position.x;
      float newY = target.position.y;
      float z = transform.position.z;
         
      float chaseSpeed = 0.05f;
         
      transform.position = transform.position + (new Vector3 (newX, newY, z) - transform.position) * chaseSpeed;   
 }
 method3(){
     cameraTarget = new Vector3 (target.position.x, target.position.y, transform.position.z);
     transform.position = Vector3.Lerp (transform.position, cameraTarget, Time.deltaTime * 4);
 }



Three solutions and problems stays same. I've recroded a movie for the effect. https://www.youtube.com/watch?v=qEuiFIqAr3o and it's not a lag - while making cam as a child it follows player as it should, but without delay.

What I wish to know, how to make it smooth? Or alternative would be, making a box, that camera stays same and when player exit that box, then it would follow player. While the second approach I haven't tried to make yet. I really wish to have working first option

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

Answer by Sarseth · Jan 14, 2015 at 10:58 PM

Ok. I have the reason. It did not have anything with camera. At least not at all. I was moving rigid body instead of transform. Anyway, moving player was:

 void Update () {
     float x = Input.GetAxisRaw ("Horizontal");
     float y = Input.GetAxisRaw ("Vertical");
     Vector2 direction = new Vector2 (x, y).normalized;
     rigidbody2D.velocity = direction * ((Input.GetButton ("Run")) ? runSpeed : walkSpeed);
 }

Now I've changed it to

 void Update () {
     float x = Input.GetAxisRaw ("Horizontal");
     float y = Input.GetAxisRaw ("Vertical");
     Vector3 direction = new Vector3 (x, y, transform.position.z).normalized;
     transform.position += direction * ((Input.GetButton ("Run")) ? runSpeed : walkSpeed) * Time.deltaTime;
 }

And it stopped jumping ;) Don't know yet much about physic in unity - still learnig so maybe it's a known thing - but for me it was new. Posting for future ;) Thanks!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Cinemachine 2d camera problem 0 Answers

Camera script 1 Answer

Shaky player effect when I apply a damping camera follow script? 1 Answer

Camera dont move whit Smooth Follow 2D 0 Answers

Why does the camera smoothly follow in one direction but not the other? 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