- Home /
Game Combat
Hello, I'm currently a first year student at college studying Computer Science. I have a project/game that I almost managed to complete, but I'm struggling to figure out how to implement combat. I tried searching the browser for answers, however the outcome was unsuccessful. I even looked at a handful of YouTube videos, which did help me with sections of the combat such as making the enemy follow the player, but I couldn't quite get them to interact and take damage from each other. I'm running out of time and ideas of how to solve this problem so I though I would get some advice from here.
I need the player to take some health from the enemy once the enemy appears on screen (and the required input has been given). Here's the player code so far:
if (Input.GetMouseButtonDown(1)) { //When enemy is facing player attack var forward = transform.TransformDirection(Vector3.forward); var distance = Vector3.Distance(transform.position, enemy.position); var targetDirection = enemy.position - transform.position; targetDirection.y = 0; var angle = Vector3.Angle(targetDirection, forward); if (distance < attackRange && angle < spotAngle) { TakeDamage.Enemy(20); //Attack(); } Ray ray = cam.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit, 100)) { Interactable interactable = hit.collider.GetComponent<Interactable>(); if(interactable != null) { SetFocus(interactable); } } }I also need the enemy to take some health from the player once they collide.
Here's the enemy code for the collision: public float initialHealth = 60f; private float currentHealth;
void Start()
{
currentHealth = initialHealth;
}
public void TakeDamage(float damage)
{
currentHealth -= damage;
if (currentHealth < 0)
{
Destroy(gameObject);
}
}
public override void Interact()
{
base.Interact();
}
void OnCollisonEnter(Collision collision)
{
if (collision.collider.gameObject.Player.CompareTag("Enemy"))
{
TakeDamage(20);
}
}
I know this is a lot to ask for, but I really don't know what to do at this point.
For better insight into what is exactly broken I recommend also putting in the class names. Regardless does TakeDamage.Enemy(20); //Attack(); actually work? It seems more likely it'd be enemy.TakeDamage(20).
Your answer
Follow this Question
Related Questions
getting udp package info inside unity (GlovePIE) 0 Answers
When the enemy character shoots, the bullet won't go to the position of my player! 2 Answers
Detect if player is in range? 2 Answers
Sprite assignments wrong in Rogue tutorial project 0 Answers
How to make a moving enemy shoot at the player rather than the player's spawn? 1 Answer