Reputation: 11
So I have a script for the player's oxygen, it's like a timer, when it reaches 0 it's game over.
I wanted the players to be able to collect oxygen in the world I created, and when they collide with the oxygen object, the slider increases, but it shouldn't go over 100.
Below is my slider code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class oxygenscript : MonoBehaviour
{
public Slider oxygenbar;
public float oxygen;
public float maxhealth = 1000;
void Start()
{
oxygen = maxhealth;
oxygenbar = GetComponent<Slider>();
oxygenbar.maxValue = maxhealth;
oxygenbar.value = oxygen;
}
void Update()
{
oxygen -= 0.1f;
oxygenbar.value = oxygen;
if (oxygen <= 0)
{
GameManager.Instance.setGameOver();
Time.timeScale = 0;
}
}
}
Upvotes: 0
Views: 733
Reputation: 31
As said with your code, your slider follows the oxygen levels. So, the only thing left is to make an oxygen point.
Your oxygen point GameObject will require a collider. Beware : you must choose 2D or 3D accordingly, they cannot collide between eachother.
Make sure your collider is a Trigger. This means your object isn't "solid", in a sense (I am assuming this is a collectible of some sort).
Add a tag to your oxygen point. Name it however you want. This will come in handy. For this example, let's use "Oxygen Point". This is much better than using direct name comparison.
When your GameObject is setup in your scene, head over to your oxygenscript and add the following function.
private void OnTriggerEnter(Collider other)
{
//If the GameObject you are colliding posesses the "Oxygen Point" tag.
if(other.CompareTag("Oxygen Point"))
{
//You increment your oxygen levels to your desired value.
oxygen += 0.5f;
//Destroy your oxygen point! Otherwise you will be able to collect it indefinetetly
Destroy(other.gameObject);
}
}
You should be alright with that. Feel free to comment if you want more details or if I am missing anything. Take care.
Upvotes: 0
Reputation: 33
To increase your oxygen slider on collision you first need to detect collisions. Both your oxygen GameObjects and Player GameObject will need to have colliders added to them in the editor. Then you will need one of them to implement an OnCollisionEnter
method. You can use the Collision
type object to check and see if you collided with what you were expecting.
The general documentation for detecting collisions on the unity docs is pretty good: https://docs.unity3d.com/ScriptReference/Collision-gameObject.html
I would recommend having your player implement the collision detection for now and move it if it becomes too cluttered. Something like this:
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.Name == "Oxygen") oxygen += <something>;
}
If you want separate Oxygen game objects to be worth different "points" then it may be easier to implement the collision detection on the Oxygen game object instead of the player. Otherwise, you'll need to do some casting to get the Oxygen GameObject type to see the specific values from it. (Which will look something like var oxygenValue = ((Oxygen)collision.GameObject).oxygenValue;
)
Upvotes: 1