Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 YingYangGujju · Dec 10, 2016 at 05:55 PM · vector3equal

Highlighting possible move locations in chess-like game

I'm trying to create a 2D chess variant game and I'm trying to make it so that when the player selects a piece, it highlights the locations on the board that the selected piece can move to. I have gotten the code to the point where it allows me to select a piece and even gives a proper list of locations that piece can move to, however, when I try to compare the Vector3's from that list to a master list of all the possible spaces on the board, it keeps returning that the Vector3 given doesn't correspond with any of the Vector3's in the master list. Here's the code I have so far: PlayerController:

 using UnityEngine;
 
 using System.Collections;
 
 using System.Collections.Generic;
 
 public class PlayerController : MonoBehaviour {
 
     private GameController gC;
     private BoardController bC;
 
     public Collider2D[] hitInfo;
 
     void Start ()
     {
         gC = gameObject.GetComponent<GameController>();
         bC = GameObject.FindGameObjectWithTag("BoardController").GetComponent<BoardController>();
     }
     
     void Update ()
     {
         if(gC.currPhase == turnPhase.playerMove)
         {
             GetInputs();
 
         }
     }
 
     void GetInputs()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Vector3 mousePos = Input.mousePosition;
             mousePos.z = 0f;
             Vector2 v = Camera.main.ScreenToWorldPoint(mousePos);
             Debug.Log(v.ToString());
             hitInfo = Physics2D.OverlapPointAll(v);
             GameObject temp = null;
             foreach (Collider2D c in hitInfo)
             {
                 if (c.tag == "WhiteStriker")
                 {
                     temp = c.gameObject;
                 }
             }
             if(temp == null)
             {
                 Debug.Log("Invalid piece selected");
             }
             else
             {
                 gC.selectPiece(temp); //GameController method that marks the selected piece as the active piece.
                 pieceType pieceName = temp.GetComponent<StrikerController>().currPiece; //enum used to identify what type of piece was selected.
                 List<Vector3> possibleMoves = new List<Vector3>();
                 switch (pieceName)
                 {
                     case pieceType.Bishop:
                         for (int i = 1; i <= 8; i++)
                         {
                             possibleMoves.Add(temp.transform.position + Vector3.left * i + Vector3.up * i);
                             possibleMoves.Add(temp.transform.position + Vector3.right * i + Vector3.up * i);
                             possibleMoves.Add(temp.transform.position + Vector3.left * i + Vector3.down * i);
                             possibleMoves.Add(temp.transform.position + Vector3.right * i + Vector3.down * i);
                         }
                         break;
                     case pieceType.King:
                         possibleMoves.Add(temp.transform.position + Vector3.up);
                         possibleMoves.Add(temp.transform.position + Vector3.up + Vector3.right);
                         possibleMoves.Add(temp.transform.position + Vector3.right);
                         possibleMoves.Add(temp.transform.position + Vector3.right + Vector3.down);
                         possibleMoves.Add(temp.transform.position + Vector3.down);
                         possibleMoves.Add(temp.transform.position + Vector3.down + Vector3.left);
                         possibleMoves.Add(temp.transform.position + Vector3.left);
                         possibleMoves.Add(temp.transform.position + Vector3.left + Vector3.up);
                         break;
                     case pieceType.Knight:
                         possibleMoves.Add(temp.transform.position + Vector3.up + Vector3.right * 2);
                         possibleMoves.Add(temp.transform.position + Vector3.up * 2 + Vector3.right);
                         possibleMoves.Add(temp.transform.position + Vector3.up + Vector3.left * 2);
                         possibleMoves.Add(temp.transform.position + Vector3.up * 2 + Vector3.left);
                         possibleMoves.Add(temp.transform.position + Vector3.down + Vector3.right * 2);
                         possibleMoves.Add(temp.transform.position + Vector3.down * 2 + Vector3.right);
                         possibleMoves.Add(temp.transform.position + Vector3.down + Vector3.left * 2);
                         possibleMoves.Add(temp.transform.position + Vector3.down * 2 + Vector3.left);
                         break;
                     case pieceType.Pawn:
                         possibleMoves.Add(temp.transform.position + Vector3.up);
                         break;
                     case pieceType.Queen:
                         for (int i = 1; i <= 8; i++)
                         {
                             possibleMoves.Add(temp.transform.position + Vector3.left * i + Vector3.up * i);
                             possibleMoves.Add(temp.transform.position + Vector3.right * i + Vector3.up * i);
                             possibleMoves.Add(temp.transform.position + Vector3.left * i + Vector3.down * i);
                             possibleMoves.Add(temp.transform.position + Vector3.right * i + Vector3.down * i);
                             possibleMoves.Add(Vector3.up * i);
                             possibleMoves.Add(Vector3.right * i);
                             possibleMoves.Add(Vector3.down * i);
                             possibleMoves.Add(Vector3.left * i);
                         }
                         break;
                     case pieceType.Rook:
                         for (int i = 1; i <= 8; i++)
                         {
                             possibleMoves.Add(Vector3.up * i);
                             possibleMoves.Add(Vector3.right * i);
                             possibleMoves.Add(Vector3.down * i);
                             possibleMoves.Add(Vector3.left * i);
                         }
                         break;
                 }
                 displayMoves(possibleMoves);
             }
         }
     }
 
     void displayMoves(List<Vector3> m)
     {
         foreach (Vector3 p in m)
         {
             Debug.Log(p.ToString());
             GameObject tileTemp = null;
             foreach (Vector3 b in bC.gridPositions)
             {
                 if(b.Equals(p))
                 {
                     Vector3 tilePos = p;
                     tilePos.z = 0f;
                     Vector2 v = Camera.main.ScreenToWorldPoint(tilePos);
                     hitInfo = Physics2D.OverlapPointAll(v);
                     foreach (Collider2D c in hitInfo)
                     {
                         Debug.Log(c.name);
                         if (c.tag == "Tile")
                         {
                             tileTemp = c.gameObject;
                         }
                     }
                     if (tileTemp != null)
                     {
                         tileTemp.GetComponent<SpriteRenderer>().color = Color.cyan;
                         tileTemp.GetComponent<Tile>().isValid = true;
                     }
                 }
             }
         }
     }
 }

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 AlwaysSunny · Dec 10, 2016 at 05:54 PM 0
Share

Looked for a $$anonymous$$ute but the problem didn't leap out. I would recommend using cell indices ins$$anonymous$$d of positions for management, but that is not necessarily part of the problem.

Try printing each Vector3 in your collection of discovered valid positions to make sure they are what you THIN$$anonymous$$ they are.

I formatted your code for you, please format it correctly in the future or everyone will point and laugh. ;)

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What happens when a Vector 3 is set equal to multiplication? 2 Answers

Vector3.Equals without memory allocation? 1 Answer

If Expression True in Immediate Window But Code In if Block Never Runs 1 Answer

Need help with rotation 4 Answers

operation between the Vector3 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