• 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 TheDreamEXE · May 24, 2015 at 02:50 PM · scripting problemscriptingbasicstileraycasthit2d

What's wrong with my raycasthit2D?

I'm making a character move on top of tiles based on a raycasthit2d that is supposed to assess each tile for various checks before actually moving the player. The problem is that I can't quite grasp the concept of Raycasthit2D for some reason (and I'm kind of dodgy when it comes to calling other scripts). Here's a bit of code that isn't functional at all, and I just can't seem to find out where things are going wrong:

UPDATED CODE STILL NOT FUNCTIONAL

using UnityEngine; using System.Collections;

public class Player1 : MonoBehaviour {

 public int playerID = 1;
 public int pHP = 1000;
 public float spotX = -4.2f;
 public float spotY = -0.25f;
 public LayerMask NotToHit;
 Transform rightPoint;


 // Use this for initialization

 void Awake ()
 {
     rightPoint = transform.FindChild("RightPoint");
 }

 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     //Vector3 spot = new Vector3( spotX, spotY, 0);

 //    Vector3 origin = new Vector3 (this.transform.position.x, this.transform.position.y, 0);
 //    origin = new Vector2(this.transform.position.x, this.transform.position.y);

     Vector2 rightFacing = new Vector2 (rightPoint.position.x, rightPoint.position.y);
 
     if(Input.GetKeyDown(KeyCode.RightArrow))
     {
         RaycastHit2D moveCheck = Physics2D.Raycast (rightFacing, Vector2.right, 2.9f, NotToHit);
         if (moveCheck.collider != null)
         {
             Debug.DrawRay(transform.position, Vector3.right, Color.cyan);

             Debug.Log(moveCheck.collider.name);
             Tile tileInfo = GetComponent<Tile>();
             Debug.Log ("Got tile comp");
             bool canMove = tileInfo.moveable;
             if(tileInfo.moveable == true)
             {
                 Debug.Log("TRUUUUUUUUUU");
                 spotX = spotX + 1.25f;
                 transform.position = new Vector3(spotX, spotY, 0);
         
             }
         }
     }


The raycasthit is supposed to assess each tile to ensure that a bool variable named "moveable" is true, but it just let's me go anywhere. I can even move off of the grid entirely. I really just need to know what's preventing this from working. Pardon the weak coding, I'm still adjusting to Unity.

UPDATE: I since created a new empty GameObject called "RightPoint" placed at the right-most corner in my Player collider with NotToHit LayerMask set to not detect the Player layer. The Player object IS in fact in the Player layer, yet moveCheck.collider.name is still returning "Player" on the console. Why is this happening?? I have no clue why it isn't detecting the tiles instead.

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

2 Replies

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

Answer by barbe63 · May 25, 2015 at 03:53 AM

That drawned my attention:

  NotToHit

It seems you have misunderstood the LayerMask. It's used to tell what layers are to hit, not the opposite. ;)

Comment
Add comment · Show 7 · 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 TheDreamEXE · May 25, 2015 at 03:57 AM 0
Share

Wait, seriously? I honestly have no clue how Layer$$anonymous$$asks are used. I spent a clean 45 $$anonymous$$ looking up examples and I ran across a Bracket's tutorial that used it the same way I did to not hit the player

avatar image barbe63 · May 25, 2015 at 04:15 AM 0
Share

I do a lot of raycasts in my project, I guarantee you it's working that way. Give it a try :p $$anonymous$$aybe Bracket did something like inversing the value of his mask somewhere?

avatar image TheDreamEXE · May 25, 2015 at 09:37 PM 0
Share

That worked! Now how exactly do I access a variable from my tile after it hits?

avatar image barbe63 · May 25, 2015 at 10:33 PM 0
Share

First declare this:

 RaycastHit hit;

Then replace your raycast with this:

 RaycastHit2D moveCheck = Physics2D.Raycast (rightFacing, Vector2.right,out hit, 2.9f, layermask); //the out will make some output you can use
 

Then right after that you can use for example this:

 if(hit.collider.gameObject.CompareTag("TagName")) //usefull to filter gameObject, you can also use layers(faster) to identify or name(slower)
 {
     value = hit.collider.gameObject.GetComponent<SomeClass>().somePublicValue;
 //or
    hit.collider.gameObject.GetComponent<SomeClass>().somePublic$$anonymous$$ethod();
 }
avatar image TheDreamEXE · May 26, 2015 at 01:47 AM 0
Share

Interesting idea. Raycast2D doesn't have an "out" parameter like the normal Raycast does though. I'm messing with my code right now trying to mimic this effect somehow. You're a huge help though :)

Show more comments
avatar image
0

Answer by maccabbe · May 24, 2015 at 02:56 PM

It seems that you have accidentally put a semicolon at the end of your if statement. To the compiler

 if(tileInfo.moveable == true);
 {
     Debug.Log("Input");
     spotX = spotX + 1.25f;
     transform.position = new Vector3(spotX, spotY, 0);
 }

reads the same as

 if(tileInfo.moveable == true){}
 
 if(true){
     Debug.Log("Input");
     spotX = spotX + 1.25f;
     transform.position = new Vector3(spotX, spotY, 0);
 }

Remove the semicolon and your if statement should work correctly

 if(tileInfo.moveable == true)
 {
     Debug.Log("Input");
     spotX = spotX + 1.25f;
     transform.position = new Vector3(spotX, spotY, 0);
 }

Side note, it is usually better to just use if(bool) instead of if(bool==true)

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 TheDreamEXE · May 25, 2015 at 03:31 AM 0
Share

whoops. Noted the semi-colon. That still didn't fix my problem though. I just have no clue what I'm doing wrong with raycasting

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can i check and fire an event when the user look at specific object ? 0 Answers

Adding callbacks/events to an instantiated prefab 2 Answers

How do I inherit certain varibles and functions from another script. 1 Answer

building error in my video project apk 0 Answers

When I try to save the velocity of a rigidbody and use it later it doesn't work? 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