Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MrBurnedPotato · Jul 22, 2014 at 07:09 PM · 2draycastspritestile

2D Tiles Raycast Problem

Hello, so I currently have a random generated world made out of 2d tiles, and I want to whenever the mouse hovers over a tile check if the tile is free which means that the tile is not closed off by other tiles so that you will be able to for example 'mine the tile away and gather it's resources'. So I want to check if the player is able to 'mine' away that certain tile only if the tile is able to be 'mined' because you don't want the player to remove tiles that are deep underneath the ground... So I made this script:

 #pragma strict
 
 var Player : GameObject;
 var hit : RaycastHit2D;
 
 function OnMouseOver () {
     Player = GameObject.Find('Player(Clone)');
     hit = Physics2D.Raycast (Player.transform.position, gameObject.transform.position);
     Debug.DrawLine(Player.transform.position, gameObject.transform.position, Color.green);
     if (hit.collider.gameObject.tag == 'Tile') {
         if (hit.collider.gameObject.transform.position == gameObject.transform.position) gameObject.GetComponent(SpriteRenderer).color = Color.green;
         else gameObject.GetComponent(SpriteRenderer).color = Color.red;
     }
 }
 
 function OnMouseExit () {
     gameObject.GetComponent(SpriteRenderer).color = Color.white;
 }

It checks if a tile is able to be 'mined' by using a raycast and then after that giving it a green color overlay indicating that it is able to be 'mined' or a red overlay indicating that it is not able to be 'mined'. But my problem is is that the ray is acting very weird, it doesn't do what I want it to do it does not give good coordinates of tiles and it just acts really strange. Please take a look at my code and tell me what problem there is,

Thanks in advance!

ALSO: The Debug.DrawLine function is not working. I don't see a green line appear on the screen so that also does not help D:

Comment
Add comment · Show 3
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 MrBurnedPotato · Jul 23, 2014 at 12:46 PM 0
Share

I have noticed that this may be a unity bug but I'm not sure, can someone help me out please?

avatar image MrBurnedPotato · Jul 23, 2014 at 12:47 PM 0
Share

Okay thank you I did not know that sorry

avatar image robertbu · Jul 23, 2014 at 01:30 PM 1
Share

I have noticed that this may be a unity bug

It is best to assume that the issue is with your code and not a Unity problem. Of the thousands of questions I've answered and the tens of thousands of questions I've read, only a couple were Unity bugs. Assu$$anonymous$$g it is your bug also keeps you looking for the answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Jul 23, 2014 at 01:21 PM

You have a couple of issues here. First, this code is not necessary. Unity uses Raycasting to determine when a mouse is over an object in order to trigger the OnMouseOver() callback. If your tile is behind other tiles (from the view of the camera), then you will not get an OnMouseOver().

The second issue is that Raycast takes a position and a direction. You are passing it two positions. The Raycast() is using the second position as a direction. There is a Physics2D.Linecast that you can use to cast a line between two positions.

And finally, you are not checking if hit.collider is null. If the Raycast() fails, then hit.collider will be null and therefore doing 'hit.collider.gameObject.tag' will generate a null reference exception. Minor point, you can do 'hit.collider.tag'.

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 MrBurnedPotato · Jul 23, 2014 at 01:41 PM 0
Share

Fixed it completely, thank you so much.

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

2 People are following this question.

avatar image avatar image

Related Questions

How to use the same tilemaps among multiple scenes (e.g. making it a prefab)? 0 Answers

Lines between Tiles when using Tile Palette (2021) 1 Answer

How to swap sprites? 1 Answer

Layer Mask on raycast2d not working? 1 Answer

Raycast for a 2D objects 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