- Home /
When the enemy character shoots, the bullet won't go to the position of my player!
When the enemy character shoots, the bullet won't go to the position of my player.
The Enemy Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed;
public float stoppingDistance;
public float retreatDistance;
private float maxDistance;
public int maxEnemyHealth;
private float timeBTWShots;
public float startTimeBTWShots;
public Transform eFirePoint;
private Transform player;
public GameObject projectile;
public float projectileSpeed;
private Vector2 target;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
target = new Vector2(player.position.x, player.position.y);
maxDistance = stoppingDistance + retreatDistance * 2;
timeBTWShots = startTimeBTWShots;
}
private void Update()
{
if(Vector2.Distance(transform.position, player.position) > stoppingDistance)
{
transform.position = Vector2.MoveTowards
(transform.position, player.position, speed * Time.deltaTime);
}
if(Vector2.Distance(transform.position, player.position) < retreatDistance)
{
transform.position = Vector2.MoveTowards
(transform.position, player.position, -speed * Time.deltaTime);
}
if(Vector2.Distance(transform.position, player.position) <= maxDistance
|| Vector2.Distance(transform.position, player.position) == maxDistance)
{
EnemyShoot();
}
}
private void EnemyShoot()
{
if(timeBTWShots <= 0)
{
GameObject eBullet = Instantiate
(projectile, eFirePoint.position, eFirePoint.rotation);
projectile.transform.position = Vector2.MoveTowards
(transform.position, target, projectileSpeed * Time.deltaTime);
timeBTWShots = startTimeBTWShots;
}
else
{
timeBTWShots -= Time.deltaTime;
}
if(transform.position.x == target.x && transform.position.y == target.y)
{
Destroy(projectile);
}
}
public void TakeDamage(int damage)
{
maxEnemyHealth -= damage;
if(maxEnemyHealth <= 0)
{
Destroy(gameObject);
}
}
}
Answer by yonatanab1 · Feb 28 at 09:11 AM
first of all, the problem could be that the player is a child of an object that affects it's position. try to print "player.position" every frame (on update()) and check if it works.
second of all, your question is a bit unclear. is the bullet moving? is it moving to the wrong direction? what is the problem here exacly? (whats working and not againts what should work)
The player is not a child of another object, and the print(player.position);
is working fine. The problem is that, when the bullet is instantiated, it does'nt move. I want the enemy to target the player and shoot, but it doesn't work.
Answer by VonKiver · Feb 28 at 01:46 PM
I'd recommend you have one class for Enemy
and another for Projectiles
Then for the target I'd have target = player.transform.position
rather than new Vector2.
Then you don't need to have if(transform.position.x == target.x && transform.position.y == target.y)
you can simply go by if (transform.position = target.position)
You also Instantiate
a GameObject eBullet
which I don't understand where's it coming from since you have a public GameObject projectile
.
But I imagine the main issue is that you don't have a method for the attack? I'm not too sure tbh but maybe something like this would help?
public void Attack()
{
Vector2 direction = player.position - eFirePoint.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
eFirePoint.rotation = rotation;
Instantiate(projectile, eFirePoint.position, eFirePoint.rotation);
}
I hope it helps.
Your answer
Follow this Question
Related Questions
How can I make the EnemyBullet go further than my target? 0 Answers
Only one cloned enemy shoots 2 Answers
How do i make chaser in 2D enless runner? 1 Answer
Enemy ai Ignore walls! help! 1 Answer
Run coroutine only when player stays inside collider? 0 Answers