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 TheHumanWall · Aug 29, 2014 at 05:16 PM · camera2d-platformersidescroller

2D camera that tracks player after running short distance

Okay, I am making a 2d sidescroller and I am having some trouble with the camera. I can make it so the camera moves along with the player, but that isn't exactly what I want.

I use the same code to move the camera(based on the current speed, acceleration, and top speed) that I use to move the player. I understand why the camera acts the way it does, but I don't know how to make it act the way I want. I have tried making the camera track slightly slower than the player, but while I know this would be good for an auto-scroller, this is not really what I want. I want the player to run a short distance AND THEN for the camera to start tracking.

for example, there would sort of be a box within screen that tells the camera to start tracking.

This way the player could run back and forth along the screen(to a certain point) without the camera moving at all. This is most common in older sidescrolling games, and I am having a hard time replicating it. Something like what I mean can be found in Super Mario World.

I suppose I just need an idea of how to do this, NOT the actual script or code.

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

Answer by robertbu · Aug 29, 2014 at 05:26 PM

Something simple like this on the camera should do the job:

 #pragma strict

 public var player : Transform;
 public var speed = 2.0;
 
 function Update() {
     var viewportPos = camera.WorldToViewportPoint(player.position);
     if (viewportPos.x > 0.75) {
         transform.Translate(speed * Time.deltaTime, 0,0);
     }
     if (viewportPos.x < 0.25) {
         transform.Translate(-speed * Time.deltaTime, 0,0);
      } 
     if (viewportPos.y > 0.75) {
         transform.Translate(0, speed * Time.deltaTime, 0);
      } 
     if (viewportPos.y < 0.25) {
         transform.Translate(0, -speed * Time.deltaTime, 0);
      } 
 }

This code uses Viewport coordinates. They start in the lower left at (0,0) and go to (1,1) for all screen resolutions/orientations. This code keeps the pivot point of the player in the middle 1/2 of the screen. You can adjust the 0.25 and 0.75 as appropriate to your game.

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 TheHumanWall · Aug 29, 2014 at 08:22 PM 0
Share

Ahhh, I did not know these existed! That is exactly what I needed. I am doing it in C# but I think I can convert it well enough.

Is there a benefit to using update() in this case over lateupdate(), I did a bit of research and I am a little confused. I don't have an update() in my camera script, just the lateupdate() because I thought that was what I was supposed to do. is this a case where there is a "proper" way to do it, but doesn't make much of a difference otherwise?

Thanks so much for the help and the reply!

avatar image robertbu · Aug 30, 2014 at 03:17 AM 0
Share

LateUpdate() is a better place for this code. The issue is that if the camera Update() is called first, then the Update() for the player, you can get some player jitter. Putting in LateUpdate() solves this potential problem. Not much to convert to C# here, but if you have issues, let me know.

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

Cinemachine Confiner 2D shift my camera view 0 Answers

How to destroy a projectile when it leaves any side of the camera view on a 2D game? 1 Answer

How to make the camera move up and down the horizontal axis only when the 2D character get's above and below the top half of the screen. 0 Answers

how to get the lowest right corner of the camera(and highest left) in units? 1 Answer

2.5D Platformer Camera follow character Y axis but not for every jump? 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