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 ApolloZC · Sep 26, 2010 at 11:04 PM · camera2djumpaxislock

How do I lock Y Axis movement for Parented Camera?

Hi, I am working on a 2D platformer and I am trying to emulate the camera movement of the original Super Mario games. The camera follows your player horizontally along the X Axis but does not travel up on the Y axis when you jump. This video shows what I mean: http://www.youtube.com/watch?v=lpk5U70MQBg

Right now I have my main camera parented to my player game object. If I jump, the camera travels up on the y axis to follow the character. I would like it so that the camera follows my player horizontally as I run left or right on the x axis, but does not raise up every time I jump.

What script could I try to keep the camera y position locked but still allow it to scroll along the x axis?

Any help would be appreciated -- thanks!

-Zach

Comment
Add comment · Show 1
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 triplev3d · Jan 23, 2015 at 07:24 AM 0
Share

Thanks $$anonymous$$rWhoof I was in the same predicament as the other guys and your script worked wonders I lost most of my hair trying to figure it out and you had it in 10 lines of code, much appreciated.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by · Sep 26, 2010 at 11:19 PM

Personally, I would suggest writing camera code at some point, rather than using a parented camera. However, it's probably not a bad place to start when prototyping a game.

You could explicitly set the y value of the camera in LateUpdate(). This would mean that every frame, after the movements have all taken place, the camera y position would be set back to the value that you want. You can use camera.main to find the main camera, and then you don't even need to put this script on the camera.

function LateUpdate()
{
   camera.main.transform.position.y = 2.0;  
}
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 ApolloZC · Sep 27, 2010 at 05:01 PM 0
Share

Thanks $$anonymous$$arowi, it worked perfectly! I'm a scripting newb, so the help is much appreciated. I'll definitely look more into controlling the camera via script (I tried a bit, like having the camera move left and right when you press the left and right buttons to move the character, but I kept running into issues). Thanks again!

avatar image · Sep 27, 2010 at 11:36 PM 0
Share

No troubles at all. Please remember to vote up answers that help, and mark them as correct (click the tick beneath the arrows) if it solves your problem. Cheers!

avatar image
0

Answer by shahinexir · Aug 10, 2012 at 08:31 PM

hey i have the same problem as u.and i used this code late update and it worked well! ;) actually im making a 2d game just like super mario.and i have a lil problems with coliding with for example walls and passing through them!! and also a lil problem on my player jumping . i really appreciate if u let me see your code for your player script . maybe it would help me.tnx alot .

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 MrWhoof · Oct 06, 2012 at 08:42 PM

If you really just need a super simple camera that follows only along X (once you move past parenting), this will do it for you, and is easy to grow from.

 var player : GameObject;
 
 var depthOffset = -10;
 var verticalOffset = 0;
 
 function Update () {
     transform.position.z = depthOffset;
     transform.position.y = verticalOffset;
     transform.position.x = player.transform.position.x;
 }

Drag the player character onto the Player variable in the Inspector, and it'll just work. Adjust your offset parameters as needed.

Comment
Add comment · Show 4 · 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 AwesomeFaceHD · Jan 21, 2014 at 03:33 AM 0
Share

This works great for me.

avatar image triplev3d · Jan 23, 2015 at 05:48 AM 0
Share

Thanks $$anonymous$$rWhoof I had the same predicament as the other guys and your code saved me from losing any more hair.

avatar image koothur19021 · Apr 05, 2015 at 02:59 AM 0
Share

@$$anonymous$$rWhoof Thanks a lot man.. I would really appreciate if you could explain this code :)

avatar image AwesomeFaceHD · Apr 05, 2015 at 03:03 AM 0
Share
@koothur19021 I'm not him, but it's just resetting the Y and Z positions to zero each frame, and updating the X position to the position of the player each frame. Super simple.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera moving in one direction? 2 Answers

How do i lock out a axis 1 Answer

2d axis is not shown 1 Answer

Parented Camera is locked to character animation 0 Answers

How can i prevent the camera from goin down the y axis (othographic Doodle jump camera) 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