- Home /
Capture the flag game with unlimited scoring,
I'm making a capture the flag game where the objective is to grab the flag and bring it back to the line. However, a player colliding with the flag or with the line will cause them to score even if they dont have the flag. This is my first game and my experience is very limited.
public float moveSpeed = 10f;
public Rigidbody2D rb;
public Collider2D coll;
public bool HasFlag = false;
public int Score = 0;
public Text scoreText;
Vector2 movement;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Flag")
{
Destroy(collision.gameObject);
HasFlag = true;
}
else if ((collision.tag == "Line") && (HasFlag = true));
{
Scoring();
}
}
private void Scoring()
{
Score += 1;
scoreText.text = Score.ToString();
SceneManager.LoadScene("SampleScene");
}
}
Answer by AlirezaSH2004 · May 18, 2020 at 07:08 AM
Hello there. In your else if statement you're actually missing one of the '='s and that is actually causing the problem, take a look:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Flag")
{
Destroy(collision.gameObject);
HasFlag = true;
}
else if ((collision.tag == "Line") && (HasFlag == true));
{
Scoring();
}
}
Answer by UnityToMakeMoney · May 18, 2020 at 07:18 AM
I think I understand your issue after reading :
"However, a player colliding with the flag or with the line will cause them to score even if they dont have the flag"
It seems that you are saying that when one player has the flag and another player is colliding with a flag or line, they are scoring even though it haven't taken the flag from them yet.
A way to fix this is to create a static variable so we know if a player picked it up.
public static bool isTaken = false;//this variable will indicate if ANY player has taken the flag
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Flag" && !isTaken)//if colliding with object tagged "Flag" and isTaken is false
{
isTaken = true;//set static variable to true - no one else can have flag
Destroy(collision.gameObject);
HasFlag = true;
}
else if ((collision.tag == "Line") && HasFlag);
{
Scoring();
}
}
The static variables make sure that any object that is a prefab or instance that uses this script knows that at some point someone else has taken the flag. This way, the HasFlag boolean will never become true unless the static variable is set back to false. In other words, every player now knows when another player has a flag.
I am not sure if you have a script for "enemy"/opposing team, but if you don't then you might have issues because that means you essentially have one team. You can ask me about when you get to that point, but I thought you should know since you might not notice as a beginner.
Your answer
Follow this Question
Related Questions
How to use the triggers on HTC Vive? 0 Answers
How to I toggle a button on/off with a button pressed? 1 Answer
Transform an object in other 2 Answers