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 /
avatar image
0
Question by Tofuburger · May 18, 2020 at 06:06 AM · scripting beginner

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");
 }

}

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
0

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();
      }
  }

Comment
Add comment · 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
0

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.

Comment
Add comment · 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

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

137 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 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

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

Transform an objects position based on camera position,Move an object based on camera position but ignore rotation 0 Answers

Public script references in the Inspector 2 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