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 Mentalist4006 · May 21, 2013 at 12:38 AM · raycastvector3tag

Tic Tac Toe--how to check for win

I am making a 3D tic tac toe, where a 3x3x3 cube holds blue and red placemarkers instead of Xs and Os. The lines can go up, down, straight, diagonal, etc. I have code that checks whether three placemarkers of the same color are in a row. Each block checks for blocks around itself. If they are the same color, a vector is made with these two blocks, creating a line. Then I use this line to raycast for another block to make a threesome. I have managed to get the script to recognize two in a row, but not three. I'm guessing it has to do with either the vectors or the casts. Here is my code(C#):

 public bool checkRow(string t){
         Collider[] col = Physics.OverlapSphere(transform.position, 0.9F);
         foreach (Collider c in col){
             if (c != collider && c.CompareTag (t)){  //check if neighbor and same color
                 Debug.Log ("Found neighbor");
                 Vector3 oneDir = c.transform.position - transform.position;
                 oneDir.Normalize();
                 Ray r = new Ray(c.transform.position, oneDir);
                 RaycastHit hit;
                 if (c.Raycast(r, out hit, 0.9F)){
                     Vector3 twoDir = hit.transform.position - transform.position;
                     twoDir.Normalize();
                     if (Vector3.Angle(oneDir, twoDir) < 5 && hit.transform.CompareTag(t)) //if the row is straight and the last one has the same color)
                         return true;
                 }
             }
         }
         return false;
     }

What could it be missing?

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

Answer by Eric5h5 · May 21, 2013 at 01:20 AM

It would be much simpler just to use an array, instead of trying to get raycasting and so on to work for this. Then you just check the relevant positions in the array. You'd only use GameObjects to reflect the state of the array, so you can dispense with colliders, physics, etc.

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 MikeNewall · May 21, 2013 at 01:39 AM 0
Share

^^ Exactly! Seems like you're over complicating things with the ray casts :p

You could even just have an array of int's to represent the board state:

 000
 000
 000

0 would represent an empty square, 1 would be a square occupied by player one, and 2 by player 2.

 102
 210
 001

The algorithm to deter$$anonymous$$e a win isn't too difficult. The only places you can win with a diagonal are from the corners, so if there was a piece there you'd check for a diagonal, and I'll let you figure out he rest :-)

avatar image Eric5h5 · May 21, 2013 at 02:00 AM 0
Share

Yep, actually I had an array of ints in $$anonymous$$d, thanks for clarifying that.

avatar image Mentalist4006 · May 21, 2013 at 11:57 PM 0
Share

Let me see if I can clarify: this is not a 3x3 board like normal tic tac toe. This is a 3x3x3 cube; you can make a line on any of the five faces--the top and the four sides--of the cube, but you can also go through the cube to make a line. The array would have to be three-dimensional, which would be at least 50 possible wins. I did raycast because it was quicker than checking each case.

avatar image Eric5h5 · May 22, 2013 at 12:16 AM 0
Share

That doesn't really change the answer; an array would be simpler and faster regardless, nor would it involve any hard-coding.

avatar image
0

Answer by Mentalist4006 · May 22, 2013 at 12:14 AM

Alright, I was finally inspired with the answer. The problem was that the raycast hit triggers that I thought would not be hit (but obviously did). So I used a layerMask to avoid this (uses Physics.Raycast() now) and increased the distance. Now it can get all the lines without having to have 50 cases for each possible line. Sorry about the confusion.

But, Eric was right; I made a three dimensional array, and had two for loops to check all the possibilities. It was way faster than my Raycast check. Thanks, Eric.

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

15 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

Related Questions

Get RaycastHit Local Coords (C#) 1 Answer

How to place an object in the direction of a raycast 1 Answer

Loop animation until character reaches hit.point 1 Answer

How to stop Raycast from camera to player target through colliders? 0 Answers

How to assign a Variable with a Raycast 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