- Home /
Enemy Projectile not flying in the right direction (needs to fly toward player),How to make enemy projectile fly toward player?
I am having trouble getting the enemy's projectile to fly from the enemy to the player's position. When I play the game, the enemy bullet projectiles fly off in one direction on the screen and not toward the player. I think the issue might be in how I am assigning direction to the projectile prefab? Any suggestions would be much appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public float speed;
public Rigidbody enemyRb;
[SerializeField] float rateOfFire;
private GameObject player;
public GameObject projectilePrefab;
float nextFireAllowed;
public bool canFire;
Transform enemyMuzzle;
void Awake()
{
enemyRb = GetComponent<Rigidbody>();
player = GameObject.Find("Player");
enemyMuzzle = transform.Find("EnemyMuzzle");
}
void Update()
{
//move enemy rigidbody toward player
Vector3 lookDirection = (player.transform.position - transform.position).normalized;
enemyRb.AddForce(lookDirection * speed);
//overallSpeed
Vector3 horizontalVelocity = enemyRb.velocity;
horizontalVelocity = new Vector3(enemyRb.velocity.x, 0, enemyRb.velocity.z);
// turns enemy to look at player
transform.LookAt(player.transform);
//launches projectile toward player
projectilePrefab.transform.Translate(lookDirection * speed * Time.deltaTime);
Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
}
public virtual void Fire()
{
canFire = false;
if (Time.time < nextFireAllowed)
return;
nextFireAllowed = Time.time + rateOfFire;
//instantiate the projectile;
Instantiate(projectilePrefab, enemyMuzzle.position, enemyMuzzle.rotation);
canFire = true;
}
}
Answer by Maggiethegsd · Sep 02, 2020 at 07:49 PM
Does the bullet need to fly directly from the enemy's body or through some gun barrell? Please provide more information.
Answer by whogas · Sep 02, 2020 at 08:03 PM
A little confused by a few things.
In your Update() loop:
*You are translating the prefab every cycle. I am not even sure what this would do. *You instantiate the prefab. I don't think you intend to do this every Update loop.
In your Fire() method:
*You instantiate the projectile prefab, but never assign it to a variable that can later be translated like you are trying to do in the Update method.
Maybe your projectile is intended to handle its motion once the object is instantiated? If it has methods that run at that point, you should start by removing the two lines I mentioned in Update. It's a bit hard to tell what your design pattern is, but those pieces look a little odd to me without knowing the rest.
Your answer
Follow this Question
Related Questions
Enemy Projectile is not flying in the right direction (needs to fly toward Player) 1 Answer
Need your help by movement 0 Answers
How do I solve this error? 2 Answers
Distribute terrain in zones 3 Answers
Projectile isn't reaching my target. 1 Answer