So basically I want so that the enemy, which is shooting the player, to deal some damage. I just don't know how to combine the two to make it work. I'll be thankful for your help. .............................................................................................................................................. Here are my Heath and bullet scripts:
Health:
public class Health : MonoBehaviour
{
public Image healthBar;
public float healthAmount = 100;
public GameObject bullet;
private void Update()
{
if (healthAmount <= 0)
{
Application.LoadLevel(Application.loadedLevel);
}
if (Input.GetKeyDown(KeyCode.E))
{
TakeDamage(20);
}
if (Input.GetKeyDown(KeyCode.T))
{
Healing(10);
}
if (bullet.)
}
public void TakeDamage(float Damage)
{
healthAmount -= Damage;
healthBar.fillAmount = healthAmount / 100;
}
public void Healing(float healPoints)
{
healthAmount = healPoints;
healthAmount = Mathf.Clamp(healthAmount, 0, 100);
healthBar.fillAmount = healthAmount / 100;
}
}
Bullet:
public class Bullet : MonoBehaviour {
float moveSpeed = 25f;
public GameObject effect;
public GameObject player;
[SerializeField] private float attackDamage = 1f;
Rigidbody2D rb;
Player target;
Vector2 moveDirection;
// Use this for initialization
void Start() {
rb = GetComponent<Rigidbody2D>();
target = GameObject.FindObjectOfType<Player>();
moveDirection = (target.transform.position - transform.position).normalized * moveSpeed;
rb.velocity = new Vector2(moveDirection.x, moveDirection.y);
Destroy(gameObject, 1f);
}
void OnCollisionEnter2D (Collision2D col)
{
if(col.gameObject.tag.Equals("Player"))
{
Instantiate(effect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
CodePudding user response:
In the Bullet script, get the Health Script from the player and call the function:
if(col.gameObject.tag.Equals("Player"))
{
if (col.transform.TryGetComponent<Health>(out Health health))
{
health.TakeDamage(50); // deal damage to player
}
Instantiate(effect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
But I have to add some Notes that I think beginners should know:
Physics in Unity is always updating in FixedUpdate ~60 times per second, independent of the framerate. You could change the physics timestep, but it's fine for most things. However if you have really fast bullets, they may pass though the player, because in one frame the bullet ist "in front of", and in the next frame "behind" the player. So you should look into raycasts to check for the next pixels in front of the bullet (depending on velocity etc.)
Also, I use "tryGetComponent<>" which is recommended, because if you use "getComponent<>" and hit an object that doesn't have a "health" component attached, you get an error. So this is the safe way to do it.
CodePudding user response:
I'm assuming you have the Health script attached to the player game object. The easiest way that you can add damage to the player would be to get a reference to the Health script you have attached to the player and use your TakeDamage function. You can do this in the OnCollisionEnter2D function attached to the bullet as follows:
playerhealth = col.GetComponent<Health>();
playerhealth.TakeDamage(5);
The end result should look something like this:
void OnCollisionEnter2D (Collision2D col)
{
if(col.gameObject.tag.Equals("Player"))
{
Instantiate(effect, transform.position, Quaternion.identity);
playerhealth = col.GetComponent<Health>();
playerhealth.TakeDamage(5);
Destroy(gameObject);
}
}
