- Home /
Hello Im looking for help creating a script for spike or fire damage!
It is a 2D platformer and I want to inflict damage to the player if he runs into or lands on fire or spikes. I'm sure its something simple but I'm really bad at scripting. Thanks in advance!
Answer by Thomas-Hawk · Mar 09, 2018 at 04:15 AM
Well, you'd use a collision callback, to trigger a method that changed the players' HP and gave a visual indication to them that the spikes hurt them.
This is the kind of thing you would be better off sitting through some tutorials on YouTube for. Like , How to make a 2D platformer, or etc, would surely cover that. You'll learn alot, and be glad you did. Scripting really isn't that hard, and most every mechanic in any game ever has some code behind it.
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Put colliders on both objects, make sure one onthe playeris a trigger,
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnTriggerEnter(Collider other) {
//if the entering object is tagged spikes
if(other.gameObject.tag == "spikes")
{
//code in these brackets is
//so if this script has an integer for HP, for example
hp-=1;
}
}
}
Thanks for the tips! I know one of the you tube videos I saw refered directly to a damage script. Is this the shell for something like that?