• 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
2
Question by MrCalavera · Apr 22, 2013 at 12:47 PM · c#2dposition

Checking to see if a position is occupied by a GameObject

Basically i've got a top down 2d view in which the player can move around. Enemies randomly spawn and will follow the player until they reach an adjacent square, where they will stop and spend subsequent "turns" attacking the player. All enemies and the player take up 1 unit of space. That part all works.

What I would like is when the player attempts to move into a square, it first checks if that square is occupied by an enemy game object. If it is occupied, the player will remain in their own square and the attack code will be carried out against the enemy. If the square is empty the player moves as normal. So basically is there an easy way to check if a position is occupied by a gameobject? And then I could do:

 if(squareIsOccupied){
 // find out what GameObject is occupying it
  // Carry out attack code against GameObject in that square.
 }
 else{
  // Carry out movement code.
 }
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 AlucardJay · Apr 22, 2013 at 12:49 PM 0
Share

raycast to that position, gather information from the hit.collider.gameObject

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by fafase · Apr 22, 2013 at 12:58 PM

This is already answered here:

http://answers.unity3d.com/questions/434100/check-if-a-spawn-point-it-occupied.html

But in case:

 var spawnPoint:Vector3 = square.position;
 var hitColliders = Physics.OverlapSphere(spawnPoint, 1);//1 is purely chosen arbitrarly
 if(hitColliders.Length > 0) //You have someone with a collider here
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
avatar image
0

Answer by UnityToMakeMoney · Mar 24, 2020 at 06:17 PM

is there a 2D version of this?

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 geanta300 · Apr 17, 2020 at 09:19 AM 0
Share

I think you can use this:

 var hitColliders = Physics2D.OverlapCircle(spawnPoint, 1);//1 is purely chosen arbitrarly

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to calculate projectile range 2 Answers

Distribute terrain in zones 3 Answers

Object instantiates on top of the previous object 2 Answers

How to get the position that is closest to a target within a given radius 0 Answers

when changing size of gameobject bounds do not change 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges