- Home /
Player Will not take any damage!
I've been working on a project for a while now, and have tried to implement the ability for the player take damage, and nothing works! I have done the exact same thing to my enemies and it works for them to take damage and die, but the player won't take any damage. Please help, thanks.
PlayerHealth Script:
public int playerHealth = 15;
public int playerDamage = 2;
public float flashSpeed = 5f;
public Image damageImage;
public EnemyMovement enemy;
bool damaged;
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
void OnCollisionEnter(Collision col)
{
Debug.Log("EnterCollider");
if (col.transform.tag == "EnemySword")
{
damaged = true;
playerHealth -= 3;
Debug.Log("DamageTaken");
if (playerHealth <= 0)
{
Destroy(gameObject);
}
damaged = false;
}
}
void Update()
{
//If the player has just been damaged...
if (damaged)
{
// ... set the colour of the damageImage to the flash colour.
damageImage.color = flashColour;
Debug.Log("FlashTrue");
}
// Otherwise...
else
{
// ... transition the colour back to clear.
damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
// Reset the damaged flag.
damaged = false;
}
}
Swing Script: { Animator anim; bool didSwing = false; public EnemyMovement enemy; AudioSource playerAudio; public AudioClip SwordSwing;
void Start()
{
//get the animator
anim = GetComponent<Animator>();
GetComponent<AudioSource>();
GameObject.FindGameObjectsWithTag("Enemy");
}
// Update is called once per frame
void Update()
{
//call triggers via keypress - you can also assign them via the preset inputs
if (Input.GetKeyDown(KeyCode.Mouse0))
{
anim.SetTrigger("swingA");
didSwing = true;
Debug.Log("swapTrue");
//playerAudio.clip = SwordSwing;
//playerAudio.Play();
}
if (Input.GetKeyDown(KeyCode.Mouse1))
{
anim.SetTrigger("swingB");
didSwing = true;
Debug.Log("swapTrue");
//playerAudio.clip = SwordSwing;
//playerAudio.Play();
}
if (Input.GetKeyDown(KeyCode.W))
anim.SetTrigger("Run");
if (Input.GetKeyDown(KeyCode.Space))
anim.SetTrigger("Jump");
}
void OnCollisionEnter(Collision col)
{
if (col.transform.tag == "Enemy" && didSwing == true)
{
enemy.enemyHealth -= 2;
if (enemy.enemyHealth <= 0)
{
Destroy (col.gameObject);
}
didSwing = false;
}
}
}
EnemyMovement Scipt:
{
public Transform Player;
public int enemyHealth = 10;
public int enemyDamage = 3;
public int MoveSpeed = 4;
public int MaxDist = 10;
public float MinDist = 1f;
bool didSwing = false;
public PlayerHealth player;
Animator anim;
void Start()
{
//get the animator
anim = GetComponent<Animator>();
GameObject.FindGameObjectsWithTag("Sword");
}
void OnCollisionEnter(Collision col)
{
if (col.transform.tag == "Sword" && didSwing == true)
{
enemyHealth -= 2;
if (enemyHealth <= 0)
{
Destroy(col.gameObject);
}
didSwing = false;
}
}
void Update()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
anim.Play("Run");
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
anim.SetTrigger("swingA");
didSwing = true;
//Debug.Log("EnemySwingATrue");
anim.SetTrigger("swingB");
didSwing = true;
//Debug.Log("EnemySwingBTrue");
}
}
}
}
The debug logs within the Health script tell me that the enemy sword is entering the collider, but no damage is being taken.
So you get the "EnterCollider" log. But do you also get the "DamageTaken" log? If not, then you have wrong tag on your enemy sword
In the documentation they use col.gameobject.tag ins$$anonymous$$d of col.transform.tag. Not sure if it will help because if transform didnt have .tag it would probably give an error. But its worth a try :p
@metalted col.gameObject.tag and col.transform.tag both respond the same way in this context - I actually opened up my editor to make sure before commenting on that lol
Answer by Larcondos · Apr 30, 2019 at 02:34 PM
As Casiell mentioned, you are getting the EnterCollider log, so that part is working properly. Since (presumably) your tag check is failing, I would suggest first making sure your enemy sword is tagged properly. If it is but you are still receiving this problem, I would add this line of code which will tell you what object is triggering your EnterCollider log.
print(col.transform.tag);
Since this will print the tag of the object that is actually triggering the event, you might be able to debug it easier.