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 primus88 · Jun 01, 2013 at 09:40 PM · cameramovementboundary

How to set boundaries with camera movement

Hello,

This is my camera movement :

 void Update () {
     var movement = Vector3.zero;
      
     if (Input.GetKey("a"))
     movement.x--;
     if (Input.GetKey("d"))
     movement.x++;
     transform.Translate(movement * speed * Time.deltaTime, Space.Self);
  
     }

I know I can limit it by using Matf.Clamp, but I don't understand how to implement it. Tried different implementation and all without success.

Any help please ?

Thanks

Comment
Add comment · Show 2
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 primus88 · Jun 01, 2013 at 10:23 PM 0
Share

I didn't. Put worked wonderfuly.

Thank you

avatar image hiddenspring81 · Jun 01, 2013 at 10:27 PM 0
Share

No problem! I've converted it to an answer. Let me know if you have any additional questions.

4 Replies

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

Answer by hiddenspring81 · Jun 01, 2013 at 10:09 PM

Have you tried

 transform.Translate(movement * speed * Time.deltaTime, Space.Self);
 transform.position = new Vector3(
   Mathf.Clamp(transform.position.x, MIN_X, MAX_X),
   Mathf.Clamp(transform.position.y, MIN_Y, MAX_Y),
   Mathf.Clamp(transform.position.z, MIN_Z, MAX_Z));

Where MIN_X, MAX_X, MIN_Y, MAX_Y, MIN_Z, and MAX_Z are your min and max values, in world space.

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
-2

Answer by Cowboykilla · Jun 01, 2013 at 10:49 PM

maybe try something like this:

 var cam : GameObject;
 
 void Start() {
 
 cam = GameObject.Find("Main Camera");
 
 }
 
 void Update() {
      var movement = Vector3.zero;
 
      if ((Input.GetKey("a")) && (cam.transform.x > -10) && (cam.transform.x < 10))
      {
           movement.x--;
      }
 
      if ((Input.GetKey("d")) && (cam.transform.x > -10) && (cam.transform.x < 10))
      {
           movement.x++;
      }
 
      transform.Translate(movement * speed * Time.deltaTime, Space.Self);
 
 }

Hope this helps.

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 3dmind · Jun 16, 2013 at 01:02 PM

Hello, i'v tried the script above`var cam : GameObject;

void Start() {

cam = GameObject.Find("Main Camera");

}

void Update() { var movement = Vector3.zero;

  if ((Input.GetKey("a")) && (cam.transform.x > -10) && (cam.transform.x < 10))
  {
       movement.x--;
  }
 
  if ((Input.GetKey("d")) && (cam.transform.x > -10) && (cam.transform.x < 10))
  {
       movement.x++;
  }
 
  transform.Translate(movement * speed * Time.deltaTime, Space.Self);
 

}` and it work as i need but the problem is when a limit is reached the script stops and camera doesn't move again to the opposite direction

Comment
Add comment · Show 1 · 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 SheZii · Feb 07, 2019 at 08:04 AM

If you want to clamp/restrict gameobjects on the boundary of Minimap, here are the links


Youtube: https://www.youtube.com/watch?v=xW4zZQhRKlI
Step by step tutorial: http://techscenarios.com/unity3d-clamp-objects-on-edges-of-minimap-clamp-on-minimap-boundary-free-scripts-part-4/

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Jerky Camera and Varying Translate speeds 1 Answer

Mech Movement/Camera Help 0 Answers

Slow camera move speed? 2 Answers

End of Animation 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