- Home /
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;
}
}
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
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.
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
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!
thank you thank you thank you, what you said worked perfectly
Your answer
Follow this Question
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