• 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 oliver-jones · Feb 13, 2012 at 02:33 AM · collidertransform.positionaxisboundary

Camera Movement Bounds?

Hello,

I have a simple camera movement script that moves on the X,Z axis (the camera is top down). I want to make the camera move within a bounds.

I first tried adding a physical collider around the border and on the camera, but the effect is kind of awful, meaning the camera just bounces off and rotates... It doesn't work basically.

I tried scripted a boundary too by doing this:

 if(transform.position.z < 20){
     transform.Translate(0, Input.GetAxis ("Vertical") * sensitivity, 0);
 }

If the camera moves further than 20, it stops on the Vertical axis like it should, but then I can't move it back down because its out of the if statement.

Any ideas? I sure would appetite it.

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 Datael · Feb 13, 2012 at 08:53 AM

I'm a little confused that you're checking the z position of the transform but then translating along Y... Is this just a mistake when you re-typed it over here?

Anyway this should be what you're looking for. I'm assuming C#... Either way this should help you get the idea.

 // define max position
 float maxZ = 20.0f; // you could even make this serialized so you can change it from the inspector later

 // figure out how much we are trying to move by
 float wantMoveZ = Input.GetAxis ("Vertical") * sensitivity;
 // make sure we're not going too far
 float moveZ = Mathf.Min(maxZ - transform.position.z, wantMoveZ);

 // finally apply the movement (if there is any)
 if (moveZ != 0) {
     transform.Translate(0, 0, moveZ);
 }

There's more easy ways to do this but I was hoping to keep it a little optimized...

This will keep the movement pinned at a max of 20. If there is no movement in Z then it will not translate the transform, etc, if for some reason it manages to go past 20 it will be reset to 20 (thanks to the min) etc etc

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Please help! I am new to unity and coding. I am trying to set my level at axis x =10.75148 and axis z= 3 but when script runs x and z axis becomes negative 1 Answer

Transforming 2D object with respect to UI 1 Answer

how do i make my cube go down the y axis? 1 Answer

How do you change the joystick axis boundary from a circle to a square? 1 Answer

Check if the center of an object is inside a trigger Collider? 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