So I want to make these spikes and I’ve placed them using a tilemap since to make my level I need to place a lot of them and what I want to do is to make the player lose a certain amount of help when they touch the spike and then gets knocked back the opposite way from the point they made contact with the spike, so the specific point on the tilemap collider. I’ve done the health loss and it works but I can’t get the knockback effect to work properly. Here’s the code attached to the spikes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpikesDamage : MonoBehaviour
{
[SerializeField] private float dmg;
public float knockbackForce;
private Vector2 moveDirection;
private GameObject player;
private void Start() {
player = GameObject.Find("Player");
}
private void OnTriggerEnter2D(Collider2D collider) {
if (collider.gameObject.CompareTag("Player")) {
collider.GetComponent<PlayerHealth>().TakeDamage(dmg);
Rigidbody2D rb = collider.GetComponent<Rigidbody2D>();
moveDirection = player.transform.position - collider.transform.position;
collider.GetComponent<Rigidbody2D>().AddForce(moveDirection.normalized * 50000f);
}
}
}
Please help.