• 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 karinsk · Oct 25, 2013 at 06:02 AM · gameobjectposition

How to Determine whether GameObject Exists in Adjacent Grid Space of Gameboard

I have a gameboard set up (like chess or checkers) and would like to determine whether another game object exists in adjacent grid spaces (and allow the selected game object to move to that position if no other object is occupying that space).

Since I am still fairly new at this, the error messages, Unity documentation, and answer boards have sent me around in circles. I'm not sure the best way to go about defining game objects and checking to see whether a user is trying to move one game object into a place where another already exists.

What I have so far is this:

 using UnityEngine;
 using System.Collections;
 
 public class MouseLook : MonoBehaviour
 {
     bool GetMouseButton;
     if(Input.GetMouseButton(0) ==  

In the above if statement, I'm thinking I can determine whether a game object exists in the position where the left mouse was clicked, but I don't know how to do that exactly. Hopefully this makes sense...In another script, I defined the grid of the gameboard, but I don't know how to call that and check whether the adjacent gridspace is occupied by a game object (and how to define the game object).

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 Owen-Reynolds · Oct 25, 2013 at 07:08 AM 0
Share

How are you with 2D arrays? Or regular ones? That's the most common way to record "who's in what square?"

Is it perfect top-down? If not, checking the square you're on is a screen-raycast. O$$anonymous$$ with those? If so, you can either use math on the hit position, or spawn a grid of tiles with a tiny "data only" script for which square they are (and that also lets you flash/color various tiles.)

avatar image karinsk · Oct 27, 2013 at 06:31 PM 0
Share

No, not perfect top-down. I imagine the camera at an angle to the board. Does it make sense to use On$$anonymous$$ouseOver in a script on the game pieces and board tiles?

It sounds like a 2D array would be useful for modeling the game board.

Thanks for your help!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Get position for each Gameobject (in array) ? 0 Answers

GameObject change Position after game started 1 Answer

Scrolling Sprite Object 0 Answers

How to re-position game object to the left side of the screen when it goes off the right side of the screen? 1 Answer

How to make an object go to pre-seted position in world 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