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 Dragonfly3r · Nov 02, 2016 at 12:18 PM · c#gameobjectrespawncheckpoints

Prevent player from activating same checkpoint again.

So I have a checkpoint system I've created based on the following criteria:

If player is within zone of the checkpoint and has atleast 1 matchstick then they can activate the checkpoint via key press of F key.

If player is within zone of the checkpoint and has zero matchsticks then they will be prevented from activating the checkpoint via key press of F key.

In this script the player is able to go to multiple checkpoints which will activate individually changing the player's respawn location.

The problem I'm facing is setting the checkpoints so if a player has say 3 matches. if they activate the checkpoint, there is no prevention to them activating the same checkpoint multiple times.

What I want to happen is if the player activates the checkpoint then if the player tries to activate the same checkpoint they will not lose any matches and will be prevented from inputting key presses on this particular checkpoint until they activate a different checkpoint and then they will be once again be able to activate the original checkpoint.

Checkpoint script

 public class Checkpoint : MonoBehaviour {
 
     public LevelManager levelManager;
     public static int _currentMatches = 3;
     public bool inZone = false;
 
     // Use this for initialization
     void Start()
     { levelManager = FindObjectOfType<LevelManager>(); }
 
     // Update is called once per frame
     void Update ()
     { if (Input.GetKeyDown(KeyCode.F) && inZone)
         { if (_currentMatches >= 1)
             { levelManager.currentCheckpoint = gameObject;
               Debug.Log("Activated Checkpoint" + transform.position); //Add gui label text display "CheckPoint activated"
               _currentMatches--;
                 Debug.Log("You have this many matches left");
                 print(_currentMatches);
             }
 
           else if (_currentMatches <=0)
             { _currentMatches = 0;
              Debug.Log("You have no matches go collect matches"); }}} // put code for when player has no matches - GUI Lavel text display "Find Matches to activate checkpoint"
     
     public void OnTriggerEnter(Collider other)  
     { if (other.name == "Player")
         { inZone = true;
          /* Debug.Log("In checkpoint zone press f to activate");*/ }}
 
     public void OnTriggerExit(Collider other)
     { if (other.name == "Player")
         { inZone = false;
           /*Debug.Log("I'm out of checkpoint zone");*/ }}
 
     public void AddMatches (int _matchesAmount) //haven't setup adding matches to current total as of yet -- Only updates checkpoint (1) never updates original checkpoint
     { _currentMatches += _matchesAmount;
         Debug.Log("You have added this match to your amount");
         print(_matchesAmount);
         Debug.Log("You have now this amount of matches");
         print(_currentMatches); }
 
     public void CheckCheckPoint()
     {
         // If player is in a checkpoint make it so they can't activate that particular checkpoint again until they've changed to a new checkpoint.
     }
 
 }

Level Manager Script - My checkpoint script communicates with this script for changing the respawn location

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class LevelManager : MonoBehaviour {
 
     public GameObject currentCheckpoint;   
     private FirstPersonController player;
 
     void Start ()
     { player = FindObjectOfType<FirstPersonController>(); }
     
     public void RespawnPlayer()
     { Debug.Log("Respawned Player");
       player.transform.position = currentCheckpoint.transform.position; }}


Does anyone have an idea as to how I can do this?

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
Best Answer

Answer by Dyougi · Nov 02, 2016 at 03:49 PM

You could verify if your current checkpoint saved instance is differente as the one the player is trying to activate.

 if (Input.GetKeyDown(KeyCode.F) && inZone && levelManager.currentCheckpoint != gameObject)
          { if (_currentMatches >= 1)
              { levelManager.currentCheckpoint = gameObject;
                Debug.Log("Activated Checkpoint" + transform.position); //Add gui label text display "CheckPoint activated"
                _currentMatches--;
                  Debug.Log("You have this many matches left");
                  print(_currentMatches);
              }
  
            else if (_currentMatches <=0)
              { _currentMatches = 0;
               Debug.Log("You have no matches go collect matches"); }}} // put code for when player has no matches - GUI Lavel text display "Find Matches to activate checkpoint"

Something like this should work.

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 Dragonfly3r · Nov 02, 2016 at 04:34 PM 0
Share

Cheers chap that works perfectly fine now I have to simply expand on that in code.

I will have a go at JincSoft solution as well and see if I can mould the two.

avatar image
0

Answer by JincSoft · Nov 02, 2016 at 02:28 PM

Probably something like

     private bool isCheckpointActive = true;
     
     void Update () { 
         if (Input.GetKeyDown(KeyCode.F) && inZone && isCheckpointActive)
         {
             if (_currentMatches >= 1)
              { 
                 isCheckpointActive = false;
 
                 //rest of your code here
              }
         }
 }

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 Dragonfly3r · Nov 02, 2016 at 04:38 PM 0
Share

Tried your solution and it doesn't work. 1 checkpoint set as default when load the scene.

The 1st solution stops me from activating it until I activate a different checkpoint.

Your solution allows me to reactivate that checkpoint once, then stops me activating it again. After which I switch to the 2nd checkpoint it will allow me to activate that once only however when I go back to the previous checkpoint I can't activate that again.

avatar image JincSoft Dragonfly3r · Nov 02, 2016 at 04:53 PM 0
Share

Well I somehow skipped over the part where you wanted it to reactivate once the player found another checkpoint, so don't worry about it, Dyougi's solution is the one you want to go with.

In regards to the activation prevention from the first solution, if you are assigning one as the default in the editor, it won't activate when you try to because it already is as far as the game is concerned. You can fix it with either public GameObject currentCheckpoint = null; or assigning a spawn point as a default only (one player can never interact with).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How do I move one of many instantiated gameobjects ONLY if it's currently selected in a list? 1 Answer

Making changes to a terrian in game 1 Answer

C# 2d Instantiate object randomly around gameobject 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