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 Jan 20, 2019 at 05:43 PM by danielg8 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by danielg8 · Jan 20, 2019 at 04:45 PM · 2d-platformerif statementflipping

If statement not working

I am trying to make it so a game object flips if the player is not in it's raycast but it does not seem to work. I know that the raycast works fine because I tested it by logging the name of the object the raycast hit, but my if statement with a boolean does not seem to work. Please help. Thanks!

     public Transform viewPoint;
     public Transform target;
 
     public float speed = 3f;
     public float viewDistance = 10f;
 
     bool FacingPlayer;
 
       void Update()
       {     
 
           RaycastHit2D hitTarget = Physics2D.Raycast(viewPoint.position, viewPoint.right);
 
         if (Vector3.Distance(transform.position, target.position) < viewDistance) 
         { 
             if (hitTarget)
             {
                 FacingPlayer = true;
             } 
             
             if (!FacingPlayer)
             {
                 Flip();
             }
         }      
     }
 
     void Flip()
     {
         transform.Rotate(0f, 180f, 0f);
     }
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 danielg8 · Jan 20, 2019 at 04:47 PM 0
Share

in other words, the Flip() function doesn't get called when it is supposed to :\

avatar image A5B · Jan 20, 2019 at 04:50 PM 0
Share

Is outer if statement evaluating true (i.e., are you sure that the distance to target is less than viewDistance)? Or is it just the inner if statements that aren't evaluating the way you want?

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by RogueMacro · Jan 20, 2019 at 04:55 PM

@goldenbergdaniel8 I believe that the second boolean statement will (almost) never run. This is because you are first checking for if the raycast was a success, not if it hit the player. I suggest you do something like this:

 if (hitTarget.collider.tag != "Player")
 {
       Flip();
 }
Comment
Add comment · Show 2 · 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 danielg8 · Jan 20, 2019 at 05:42 PM 0
Share

Thanks so much! I just tried it an it worked!

avatar image RogueMacro · Jan 20, 2019 at 05:51 PM 0
Share

Happy to help :)

avatar image
0

Answer by Bunny83 · Jan 20, 2019 at 05:21 PM

This statement:

 if (hitTarget)

is pointless since hitTarget is a struct. This statement will always be true. You may pay more attention to the example given in the documentation. You have to check if hitTarget.collider is null or not. If it's null, nothing was hit. If it's not null it contains a reference to the collider that was hit.


So it should be

 if (hitTarget.collider != null)
 {
     FacingPlayer = true;
 }

Note that you never set FacingPlayer back to false. So once this if condition is true, FacingPlayer will be set to true and stay true until you explicitly set it back to false. In the code you've posted you never set it back to false.

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 unityBerserker · Jan 20, 2019 at 06:58 PM 0
Share

RaycastHit2D has an operator for conversion to bool. There is no need to check if we hit a collider.

public static implicit operator bool (RaycastHit2D hit);

Follow this Question

Answers Answers and Comments

110 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Hi, just a small question. Why isn't my enemy flipping? I know I'm doing this right because my player if flipping, but what about my enemy. I'm getting really frustrated here. 0 Answers

Would love some advice, based on a reply of yours about 2D sprite flipping. (U5.3) 0 Answers

Flip Player, Not the Player's Child. 3 Answers

(2D sidescrolling platformer) Flipping my character right or left depending on the mouse? 2 Answers

Player Flipping and Shot/Tilemap bugs 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