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 drodrii · Mar 10, 2014 at 11:23 PM · movementtransformspritepositionvector3

2D Background Stop Moving on BoundingBox Exit?

Hello everyone, I currently have an Object that's moving around a background(which is a sprite.) This is how the object moves.

The idea of movement is Gameboy Zelda style. I've created a small HUD with arrows.

(the movement script is attached to the player...)

If up arrow is clicked

 transform.Translate (Vector3.up * speed * Time.deltaTime);


If down arrow is clicked

 transform.Translate (Vector3.down * speed * Time.deltaTime);

If left arrow is clicked

 transform.Translate (Vector3.left * speed * Time.deltaTime);

and so on....

Now I want to accomplish : - Don't move the player if the limits of the bounding box of the Background sprite are reached.

I haven't started to do any scripts because I honestly am blank on how to start tackling this problem thats why I come to you for help.

If you think I should change the way the object moves please let me know why, and what to modify it to.

Thank you for your time and hope hearing for your answers.

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

2 Replies

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

Answer by Quokmoon · Mar 11, 2014 at 04:36 AM

1.You can put validation bound using Mathf.Clamp

Example :suppose If you pressed up arrow Object transform should be:

 Vector3 pos = target.position;
 pos.y = Mathf.Clamp(pos.y, 2.0f, -2.0f);
 target.position = pos;
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 drodrii · Mar 11, 2014 at 08:03 PM 0
Share

@Quokmoon && @robertbu

So I tried robert's solution first, didn't notice he answered. So I tried Quokmoon's solution then.

Here's what happens the user presses Up arrow and we get gui.upClicked == True, hence moving WHILE the button is pressed but it went insane on me. I click up arrow and switches between maxY and $$anonymous$$Y....What is it Im doing wrong? The idea is that when the player reaches 16 it stops moving....

 Vector3 tempPosition;
 
     private float $$anonymous$$Y = -16f;
     private float maxY = 16f;
     private float $$anonymous$$X = -32f;
     private float maxX = 32f;
 
 
 public void UpDownLeftRight()
 
     {
         if (gui.upClicked == true) 
         {
             
             
             //$$anonymous$$ove Zombie
             transform.Translate (Vector3.up * speed * Time.deltaTime);
 
             /*transform.position.x = $$anonymous$$athf.Clamp(transform.position.x, $$anonymous$$X, maxX);
             transform.position.y = $$anonymous$$athf.Clamp(transform.position.y, $$anonymous$$Y, maxY); */
 
              tempPosition = transform.position;
             tempPosition.y = $$anonymous$$athf.Clamp(tempPosition.y, maxY, $$anonymous$$Y);
             transform.position = tempPosition;
             
             
             
             
             gui.upClicked = false;
             
         }
 }
avatar image drodrii · Mar 11, 2014 at 09:27 PM 0
Share

nvm maxY and $$anonymous$$inY were mixed up hahaha, thanks for your time guys!

avatar image
0

Answer by robertbu · Mar 11, 2014 at 04:10 AM

Since this is a 2D game, the easiest solution is to clamp the position. Move your character to the maximum limits x and y and record the positions. Then just below your transform.Translate()s above, do:

 transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);
 transform.position.y = Mathf.Clamp(transform.position.y, minY, maxY);
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 robertbu · Mar 11, 2014 at 04:48 AM 0
Share

@$$anonymous$$ii - If you are coding in C#, the look at Quokmoon's answer. $$anonymous$$y answer will only work in Javascript since in assigns individual components of transform.position. In C# you have to update the whole position.

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

21 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 avatar image avatar image

Related Questions

How to smooth movement object in one-axis? 1 Answer

Setting target position in Vector3.MoveTowards 2 Answers

Moving an Object - The right way. 2D 1 Answer

Creating a bouncing game without using physics - Vector3 math problem, 2 Answers

Trouble setting up a vector 3. 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