Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by animeman0 · Jul 14, 2015 at 04:16 PM · c#booleanxaxis

Boolean flickers true and false - c#

In my game when the enemy detects the player from behind them the boolean facingRight should equal true and the enemy should flip 180 degrees on the x axis and run towards the player.

the enemy runs towards the player however facingRight flickers true and false which makes the enemy Flip constantly!!

 if(seePlayer != null)
             {
                 Debug.Log("worked");
                 anim.SetFloat("Speed",Mathf.Abs(1));
                 rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
                 facingRight = true;
                 if (facingRight)
                 {
                     Flip();
                     facingRight = false;
                 }
             }


Full Code:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : MonoBehaviour {
 
     bool seenPlayer;
     RaycastHit2D seePlayer;
 
     public Rigidbody2D rb;
 
 
     public Vector2 newPos;
     public Vector2 negativeNewPos;
 
     float maxSpeed = 3f;
     Animator anim;
 
     GameObject player;
     public bool facingRight = false;
 
     public float sightDistance;
 
 
 
     void Start()
     {
         anim = GetComponent<Animator> ();
         rb = GetComponent<Rigidbody2D> ();
         player = GameObject.FindWithTag("Player");
     }
 
     void Update()
     {
 
     }
 
     void FixedUpdate()
     {
         //Just a debug visual representation of the Linecast, can only see this in scene view! Doesn't actually do anything!
         Debug.DrawRay(transform.position, newPos , Color.magenta);
         Debug.DrawRay(transform.position, negativeNewPos , Color.magenta);
         
         if(Physics2D.Raycast(transform.position,newPos,sightDistance, 1 << 8))
         {
             seePlayer = Physics2D.Raycast(transform.position,newPos, sightDistance, 1 << 8);
             seenPlayer = true;
             if(seePlayer != null)
             {
                 Debug.Log("worked");
                 anim.SetFloat("Speed",Mathf.Abs(1));
                 rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
                 facingRight = true;
                 if (facingRight)
                 {
                     Flip();
                     facingRight = false;
                 }
             }
         }
         else if(Physics2D.Raycast(transform.position,negativeNewPos,sightDistance, 1<< 8))
         {
             seePlayer = Physics2D.Raycast(transform.position,negativeNewPos,sightDistance, 1 << 8);
             seenPlayer = true; //since the linecase is touching the player and we are in range, we can now interact!
             facingRight = false;
             if(seePlayer != null)
             {
                 Debug.Log("worked");
                 anim.SetFloat("Speed",Mathf.Abs(1));
                 rigidbody2D.velocity = new Vector2(-maxSpeed, rigidbody2D.velocity.y);
                 facingRight = false;
                 if (facingRight)
                 {
                     Flip();
                 }
             }
         }
         else
         {
             seenPlayer = false; //if the linecast is not touching a guard, we cannot interact
             anim.SetFloat ("Speed",0);
         }
     }
     void Flip ()
     {
         facingRight = !facingRight; // flips the character
         Vector3 theScale = transform.localScale; //flips the local scale
         theScale.x *= -1; //flip the x axis 
         transform.localScale = theScale; //apply all of this back to the local scale
     }
     void movingRight()
     {
         Vector2 moveRight = transform.localScale;
         moveRight.x *= -1;
         transform.localScale = moveRight;
     }
 }

Comment
Add comment · Show 1
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 Spartanias · Jul 15, 2015 at 07:39 AM 0
Share

I'm very new to unity and C# but what Infinibuzz suggested looks correct. A flag is basically a yes/no type of variable.

For the below, i want my flag variable, flg, to turn on when x happens to be 5. If x =5 then flg = true If x <>5 then flg = false

1 Reply

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

Answer by InfiniBuzz · Jul 14, 2015 at 04:23 PM

Hi

some parts of your code don't make sense. You check for facingRight == true right after you set it to true.

You can use a flag that indicates if you have flipped already.

 bool flipped = false; // define flipped-flag somewhere
 ...
 ...
 if(seePlayer != null)
 {
      Debug.Log("worked");
      anim.SetFloat("Speed",Mathf.Abs(1));
      rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
      // facingRight = true;
      if (!flipped)
      {
           Flip();
           facingRight = true;
           flipped = true;
      }
 }

Not sure if it will work, probably depends on the rest of your code.

Edit, rethought: Just dont change the flipped right variable and don't check it.

 if(seePlayer != null)
 {
      Debug.Log("worked");
      anim.SetFloat("Speed",Mathf.Abs(1));
      rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
      facingRight = true;
      Flip();
 }

Note However if you have this codeblock in the Update() Function you need the first attempt I posted because otherwise it will still constantly flip.

Comment
Add comment · Show 4 · 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 animeman0 · Jul 14, 2015 at 04:41 PM 0
Share

I set it to true because he is detecting him from the right side, and so i checked to see whether he was facingRight in order for me to flip it.

Also what's a flag, I'm still new to this

avatar image InfiniBuzz · Jul 14, 2015 at 04:56 PM 0
Share

All right. With 'flag' I just mean a variable (e.g. bool) which is used to remember a state - in your case if the player already flipped or not. Did the answer help? If it solved your problem you can tick it as the correct answer. Also remember that you can up/downvote questions and answers as well as comments in order to help the answers community. Welcome to unity and unity answers!

avatar image animeman0 · Jul 14, 2015 at 05:06 PM 0
Share

So would it be

bool flipped = facingRight;

avatar image animeman0 · Jul 14, 2015 at 05:39 PM 0
Share

thank you thank you thank you, what you said worked perfectly

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Prevent one from doing action if one is activated (C# UNITY) 1 Answer

Can't use script name as variable type? 2 Answers

Input.GetMouseButton NEVER returns false 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