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 /
This question was closed Sep 03, 2015 at 01:48 AM by peacemaker109 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by peacemaker109 · Sep 03, 2015 at 05:06 AM · c# tutorialgameobject.findgameobject

C# to find a Object in a direction on a 2D map

So I have a simple 2 layout, that i have a map generator placing a bunch of random blocks around, i set the generator to name each block based on its tile/array position, so Block at 1x1, Block at 1x9, etc. I have the player walking around ontop of the blocks, and want the player to be able to interact with blocks based on what direction there keys are, for example I hit Space, while moving to the Left, if there is a block to the left of me, runs a function attached in that blocks scripts, like if im holding a pick axe, and moving to the left while pressing space, it will try to mine that block. I thought using a 2d box colider as a trigger, but seems to make the player run the script on everything, so the ground there standing on, moving on, and if one is above also mines it. Is there a way to use like a raycast or something and say cast it from the player in the direction there moving, get that object, and have it work properly in a 2D game or is raycasting for 3D? or any other decent methods of finding only a single object in that direction closest to the player?

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 peacemaker109 · Sep 03, 2015 at 01:30 AM 0
Share

The reason I ask on the raycast (i have tried) it seems to always 'hit' the player, the code i tried with the raycast

     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
     { // use tool based on direction moving
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow))
         {
             RaycastHit2D hit = Physics2D.Raycast(player.transform.position, -Vector2.up);
             if (hit.collider != null)
             {
                 print ("Block hit: " + hit.collider);
             }
         }
     }

Every Print it does says player no matter how close I am to any other block

avatar image peacemaker109 · Sep 03, 2015 at 01:47 AM 0
Share

:) nvm i fixed it, hope it works in the end not just as a temporary thing, but by going to edit-project settings - physics2d, and turning off the Raycast hit triggers, seems to skip the first one( player) and return the block right next to them :)

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Advice for 3rd Person Platformer 1 Answer

How to save coins with PlayerPrefs 2 Answers

How can I access Virtual Camera on the script for the Cinemachine shot in the time line track? 0 Answers

Find a GameObject without a reference 2 Answers

ScrollableList best way to implement 0 Answers


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