How to have a clock ticking down when my player collides with something
Hi so I only know the basics about scripting but I need to make a clock for my game. I have looked around a bit and nothing really fits my need or im just stupid and dont understand it. So if you could explain this too me like im 5, but once my player collides with something I need to have some text show up and start counting down from 30. While I have a basic idea of what it would look like (something like the code I have at the bottom?) I am not sure how to do the clock component and tune it to my needs.
void OnTriggerEnter (Collider other)
{
if(gameObject.CompareTag("Pickup")
{
//start clock here?
}
Allow me to elaborate a bit more; When my gameobject(the player) collides with an object with the tag Pickup I want the script to do 3 things, Switch Cameras (which I havent gotten to work but this is my code below ↓, maybe someone could help me fix it, What happens is that well nothing happens. The camera1 doesnt enabled and camera2 doesnt disable. Here is a vid displaying it: https://www.youtube.com/watch?v=x6EcGPt71qg&feature=youtu.be )
using UnityEngine;
public class idk : MonoBehaviour
{
public Camera camera1;
public Camera camera2;
void OnTriggerEnter(Collider other)
{
if (gameObject.CompareTag("Pickup"))
{
camera1.enabled = true;
camera2.enabled = false;
}
}
}
And then I want the countdown timer to begin starting at 30 seconds. And When it hits 0 I would like the scene to change(I dont really know how to do that. I know the code for switching scenes but im not sure how to implement it here.)
Answer by bokchoy4221 · Dec 29, 2016 at 09:37 AM
i think it would be something like this. You would need to assign the countDownText Text to UI Text object in your game.
public int countDown = 30;
public Text countDownText;
void OnTriggerEnter(Collider other)
{
if (gameObject.CompareTag("Pickup")
{
countdown -= time.deltatime;
countDownText.text = " " + countdown;
}
}
I get a ton of errors when using your script. But 2 pro$$anonymous$$ent ones are countdown is a type but is used like a variable, namespace text could not be found and countdown is not valid in the given context
Answer by UnityCoach · Dec 29, 2016 at 10:39 AM
You need to add :
using UnityEngine.UI;
at the beginning of your script, as Text is part of it.
Also, you should change countDown to type float, it'll never go down otherwise, unless your framerate is so slow that deltaTime is greater than 0.5.
public float countDown = 30.0f;
One more thing, gameObject refers to the GameObject you put that script on. You may want to change this to:
if (other.gameObject.CompareTag("Player")
So that only the Player triggers the collection of the item.
I used all your suggestions except the last one since im putting the script on a player than when colliding with a object with the tag Pickup the countdown timer begins.
But what happened was that once the player collides with the Pickup object nothing happens. I wanted the text to update and show that its counting down, i forgot to put this into the first question. Also I only want it to show once the gameobject collides with the pickup.
You'll have to use
if (other.gameObject.CompareTag("Pickup"))
{
}
gameObject otherwise refers to the gameObject the component sits on. Also, note that, as opposed to OnCollisionEnter(), the OnTriggerEnter message is only sent to the Trigger Collider. So, in this configuration, your Player must have a Collider set as "Trigger" and the Pickup must have a standard Collider. I usually do this the other way around, so that my Player can still collide with other objects with real collisions. $$anonymous$$y two cents.
Yeah I had that in my code already, all I need help with is getting the text to update and count down from 30
Which one has more benefits coruntine or boolean?
If you want a "tick" every second for no longer than 30 seconds, I'd say go for the Coroutine. Performance wise, putting an if statement in Update probably has the same cost.
The best option would be to have a Component that does just that, then you don't put an if statement in Update, you simply turn it 'Off' on Awake, 'On' on OnTriggerEnter, and 'Off' again at the end of the countdown.
Another question comes to my $$anonymous$$d ; will the player be able to pickup several items, and what happens with the countdown then? If so, the Coroutine will have to be stopped/restarted, while the Update may just reset the countdown value.
No the player will not be able to pickup several items, but I have to go know so I will respond to your answers later on when I get the chance.