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 DevMerlin · Dec 07, 2012 at 11:33 AM · physicsgridtiles

How do I prevent players from turning unless they are in the center of a tile?

I have a physics-based game, that is at this point mostly finished. However, the game plays out on a grid and I'd like to keep players in the center of the grid tiles. I know the spacing and number of tiles and can apply that, and my movement/engine code is already finished:

     void LateUpdate() {
         float throttle = Input.GetAxis("Vertical");
 
         
         
         if (Input.GetAxisRaw("Horizontal") < 0)
         {
             StartCoroutine(RotateObject(new Vector3(0, 270, 0), 0.4f));
         }
         
         if (Input.GetAxisRaw("Horizontal") > 0)
         {
             StartCoroutine(RotateObject(new Vector3(0, 90, 0), 0.4f));
         }
 
     
 
         
         Vector3 basePosistion = Vector3.forward;
         
         if (throttle > 0)
         {
             basePosistion = rigidbody.rotation * basePosistion * throttle;
         } else {
             basePosistion = rigidbody.rotation * basePosistion * 0;
         }
         
         rigidbody.velocity = basePosistion * AmbientSpeed;
         captureTest();
     }

How would I determine where the player is on a tile? My suspect feeling is that I can use a magnitude test to prevent them from turning, but other than that I am not sure.

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 DevMerlin · Dec 07, 2012 at 03:22 PM 0
Share

Anyone? Trying to get this solved quickly.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Dec 07, 2012 at 03:53 PM

Use standard mapping functions to go from tile# to worldPos, and worldPos to tile#. Say your tiles are 5 units wide. That means the center of tile#X is at world 5X+2.5. The other way, from world to tile, is (worldX-2.5)/5.

Check that: Tiles are centered at 2.5, 7.5, 12.5, 17.5... . if you are at 14, you are on the right side of the 10-15 tile (which is #2). Math gives you (14-2.5)/5 = 2.3. Meaning you on tile#2, right side.

The "best" numbers are whole numbers, meaning you are dead center of a tile The worst end in 0.5, meaning you are 1/2-way between two tiles. So just check your tile# is "close enough" to a whole number. If you do it for X and Z, you'll get a square that counts as "in the center." For a circle, could square the results instead.

Comment
Add comment · Show 5 · 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 DevMerlin · Dec 07, 2012 at 04:09 PM 0
Share

In this case my tiles are 14 units in size. I have the current tile via the formula position.x/y / spacing, or transform.position.x / 14. That gives me the edge as an int. So to get the correct magnitude for the center, I'd add + 7 to a float?

avatar image Owen-Reynolds · Dec 08, 2012 at 03:29 AM 0
Share

Try any formula, get some paper and test out some numbers.

If you want to just divide by 14, like you say, whole numbers are the edges. So check the fraction is "about 0.5". If you'd rather check feet, take the fraction times 14. That will give you how many feet from the edge.

avatar image DevMerlin · Dec 08, 2012 at 04:32 AM 0
Share

float xTest = transform.position.x / gameController.spacing; I currently have this to test for tiles, but it gives me mostly whole numbers. .spacing is = 14. It seems to be almost the formula you described.

avatar image Owen-Reynolds · Dec 08, 2012 at 02:50 PM 0
Share

"$$anonymous$$ostly whole numbers"? Are you saying that at 21, it correctly gives you 1.5, but then when you move to 26 it suddenly rounds up to 2? $$anonymous$$eep testing, pause (near tile center & at edge) and hand-check values.

If you're using `Debug.Log` it often rounds to the nearest tenth, so standing at 42.5 might print 3.0.

avatar image DevMerlin · Dec 08, 2012 at 03:53 PM 0
Share

I misspoke, the version in the control had (int) cast. Anyway.. this isn't working to produce what I need. I'm looking at other methods such as false pressure plates now.

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

10 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

Related Questions

Can someone help me to find what I am doing wrong?(code attached) 1 Answer

How to find a circle (radius) in grid tiles around another tile 1 Answer

Correct use of setTile to add tile to Tilemap 0 Answers

Texture grid displayed oddly when width =/= height 1 Answer

Bad grid rigibody3D collision 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