Question by
matthewbev12 · Dec 14, 2018 at 06:44 PM ·
gameobjectdistancedamageattack
Player Can't hit enemy
Hello I've gotten into game development recently, and I'm looking to make my player be able to hit enemies within a certain range. My weapon doesn't seem to attack the enemy when I press the attack button, and Increasing the "minDist" makes the enemy take damage at any range.
This is what I have so far:
public GameObject weapon; private float nextAttack; private float attackDelay = 0.3f; public static Hit Instance; void awake(){ Instance = this; }
void Update()
{
float dist = Vector3.Distance(weapon.transform.position, transform.position);
float minDist = 2f;
if (Input.GetKey (KeyCode.R)&& Time.time > nextAttack) {
nextAttack = Time.time + attackDelay;
if (dist < minDist) {
Attack ();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Get the lowest value from a Gameobject List 0 Answers
Damage drop-off over distance on a physical projectile 1 Answer
Timer-Based Attack System 0 Answers
how to use vectors to control an outcome 0 Answers