- Home /
How can I make the EnemyBullet go further than my target?
I want to make the bullet, shot by my enemy, go further than the position of my target. The player character. But how do I do that?
The code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyProjectile : MonoBehaviour
{
public float projectileSpeed;
public int projectileDamage = 30;
private HealthManager healthManager;
private Transform player;
public Vector2 target;
void Start()
{
healthManager = GameObject.FindObjectOfType<HealthManager>();
player = GameObject.FindGameObjectWithTag("Player").transform;
target = new Vector2(player.position.x, player.position.y);
}
void FixedUpdate()
{
transform.position = Vector2.MoveTowards(transform.position, target, projectileSpeed * Time.deltaTime);
if(transform.position.x == target.x && transform.position.y == target.y)
{
DestroyProjectile();
}
}
//Deal Damage
void OnTriggerEnter2D(Collider2D coll)
{
if(coll.gameObject.CompareTag("Player"))
{
healthManager.TakeDamage(15);
DestroyProjectile();
}
}
void DestroyProjectile()
{
Destroy(gameObject);
}
}
Comment
Your answer
Follow this Question
Related Questions
My enemy character is not facing the player correctly! 0 Answers
How to make my object go in the direction its facing [C#] 3 Answers
Tweaking character movement 1 Answer
Making player jump without rigidbody or character controller 1 Answer
Rotate game object and then return to its original rotation 1 Answer