- Home /
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?
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.
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.
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
}
}
}
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.
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
