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 /
  • Help Room /
avatar image
0
Question by Brutalitywarlord · May 12, 2016 at 02:40 PM · c#collision2d gamecheck

Checking for a gameobject at a position

I am trying to make a spawner button for my 2D-TBS game, i have gotten the object to spawn, but i am still trying to make a check to see if there is a game object at that location, other wise i get objects overlapping and pushing each other out of the way, i am drawing a complete blank on how to do it code wise, but i do know i need colliders, so i have place Boxcollider2Ds on everything thats gonna collide with something on the same layer

my code is included below, my objective here is to check if there is a gameobject at a current position, and if there is an object at that position, to spawn the object at a different position instead,

using UnityEngine; using System.Collections; using UnityEngine.UI; using Random = UnityEngine.Random;

public class Mspawn : MonoBehaviour { public Button button; public BoardManager boardManger; public int x; public int y; public Vector3 CheckPosition; void OnEnable(){ if (Input.GetMouseButtonDown(0)) { button.onClick.AddListener (SpawnUnit); }

 }
 void awake(){
     boardManger = GetComponent<BoardManager> ();
 }
 void SpawnUnit()
 {
     /*Debug.Log ("Testing...");
     for (int i = 0; i < boardManger.PlayerUnits.Length; i++) 
     {
     //this code is dysfunctional, im still working on it. just ignore it
         Debug.Log (boardManger.PlayerUnits.Length);
         if (boardManger.PlayerUnits[i] == GameObject.FindWithTag("PlayerSoldierMarksman"))
         {
             Debug.Log("Testing complete");
             Instantiate (boardManger.PlayerUnits[i], new Vector3 (1, 1,0f), Quaternion.identity);
         }
     }*/
     Instantiate (boardManger.PlayerUnits[3], new Vector3 (randomSpawnPosition), Quaternion.identity);
 }
 public void checkPosition(Vector3 check)
 {
     check = randomSpawnPosition ();
          //this is obviously the location for what i'm trying to do;
 }
 Vector3 randomSpawnPosition()
 {
     int x = Random.Range(0,2);
     int y = Random.Range(0,2);
     Vector3 Position = new Vector3 (x, y);
     return Position;

 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jordi-Bonastre · May 12, 2016 at 03:48 PM

I think that you can use Physics.Linecast from top to bottom, assigning the correct layer and check if there is any collision with a player or whatever are you instantiating. Maybe there are options more elegant, but this is what I should do in 5 minutes. http://docs.unity3d.com/ScriptReference/Physics.Linecast.html

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 Brutalitywarlord · May 13, 2016 at 02:46 PM 0
Share

this seems a little overkill for checking a single square beside me for a collision

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Collision inside a local function 2 Answers

Objects not colliding 1 Answer

Reach other object animator when collide and play animation 1 Answer

How can i put down a sprite on collision? 0 Answers

How do I change level/scene? 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