I’m a beginner that have started on a game, and have worked on it for like 3-4 days.
My question is, how can I make my cube dealing damage on my character on collision? I have maked a script so it will deactivate when the character touches it.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText();
}
if (other.gameObject.CompareTag("Pick Up 1"))
{
other.gameObject.SetActive(false);
}
Pick Up is the spheres that give me points. And I want the Pick Up 1 to make damage to me when I touch it.
The game I’m working on is a ball game.
I have also made a health bar in another script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthBar : MonoBehaviour{
public float CurrentHealth { get; set; }
public float MaxHealth { get; set; }
public Slider healthbar;
void Start ()
{
MaxHealth = 4f;
// Resets health to full on game load
CurrentHealth = MaxHealth;
healthbar.value = CalculateHealth();
}
void Update ()
{
if (CurrentHealth > MaxHealth)
{
CurrentHealth = MaxHealth;
}
}
void DealDamage(float damageValue)
{
// Deduct the damage dealt from the character's health
CurrentHealth -= damageValue;
healthbar.value = CalculateHealth();
// If character is out of health, die!
if (CurrentHealth <= 0)
Die();
}
float CalculateHealth()
{
return CurrentHealth / MaxHealth;
}
void Die()
{
CurrentHealth = 0;
Debug.Log("You dead!");
}
}
I would love to get help!!
I need to know how to make the cube “Pick Up 1” deal damage to my charachter when it touch it (and still disappear when the ball touches the cube.) And I also want to make so the health bar works with it.
Please read this page for posting code nicely on the forums: Using code tags properly
You can edit your post & keep it in mind for future postings.
Change the DealDamage method to be public.
Then create a reference for HealthBar script on whichever script your first one was.
Now, when you run into pick up 1, do something like:
myHealthBar.DealDamage(5); // where 'myHealthBar' is the variable for your health bar script that you created and assign by drag & drop in the inspector***
Like Fox says, definitely go through the beginner section of the tutorials. Specifically to do with scope, and accessing classes from different classes. That said, let me give you a helpful nudge:
public class Player_controller : MonoBehaviour
{
public float speed;
public int jumpspeed = 0;
public Text countText;
public HealthBar healthBar;
private Rigidbody rb;
private int count;
private bool istouching = true;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
}
void Update()
{
}
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
if ((Input.GetKey(KeyCode.Space)) && istouching == true)
{
Vector3 balljump = new Vector3(0.0f, 6.0f, 0.0f);
rb.AddForce(balljump * jumpspeed);
}
istouching = false;
}
void OnCollisionStay()
{
istouching = true;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
if (other.gameObject.CompareTag("Pick Up 1"))
{
healthBar.DealDamage(1f);
other.gameObject.SetActive(false);
}
}
void SetCountText()
{
countText.text = "Score: " + count.ToString() + "/46";
}
}
The magic to make a method public is to put the public keyword at the beginning. Like this:
public void DealDamage(float damageValue)
{
// Deduct the damage dealt from the character's health
CurrentHealth -= damageValue;
healthbar.value = CalculateHealth();
// If character is out of health, die!
if (CurrentHealth <= 0)
Die();
}
In your player controller your FixedUpdate() method is set as private. That means no class outside of player_controller can use that method. When there is no private/public at the beginning, the code will default to private. Writing public at the beginning means any class outside that class can call that method.
Lastly where I’ve added the HealthBar variable in Player_Controller, you’ll want to find the HealthBar in the scene, and drag it onto the HealthBar variable on Player_Controller in the inspector.
That’s my good deed for the day. Make sure you watch the tutorials. You will get there :).
Thanks for all help! I have checked the whole “Roll-a-ball tutorial”. Because it’s a ball game I’m making! It was really great. And because of your help, I could make so the player takes damage when it falls of the map!
I also have seen many tutorials on youtube! It have helped me alot with the game!