I have a few scripts that are supposed to make the enemy shoot witch they do but not in the way I expected. The bullets travel in an arc instead of directly at the player here are the scripts:
this is the weapon the enemy uses witch instantiates the bullet.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class EnemyPistol : MonoBehaviour
{
public float offset;
public AudioSource Shoot;
public AudioSource ReloadSFX;
public GameObject EnemyProjectile;
public Transform shotPoint;
public Transform player;
private float shotCooldown;
public float startTimeForCooldown;
void start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
shotCooldown = startTimeForCooldown;
}
void Update()
{
if (shotCooldown <= 0)
{
Instantiate(EnemyProjectile, shotPoint.position, Quaternion.identity);
shotCooldown = startTimeForCooldown;
}
else
{
shotCooldown -= Time.deltaTime;
}
}
}
This script handles the projectile and is supposed to make it move to the player’s last position, instead, it travels in an arc and freezes once it gets above the player and it doesn’t trigger the DestroyProjectile(); function. Can anyone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyProjectile : MonoBehaviour
{
public float speed;
public float lifeTime;
public float distance;
public int damage;
public Transform player;
private Vector2 target;
public LayerMask whatIsSolid;
public GameObject destroyEffect;
void Start()
{
Invoke("DestroyProjectile", lifeTime);
player = GameObject.FindGameObjectWithTag("Player").transform;
target = new Vector2(player.position.x, player.position.y);
}
void Update()
{
transform.position = Vector2.MoveTowards (transform.position, target, speed * Time.deltaTime);
if(transform.position.x == target.x && transform.position.y == target.y)
{
DestroyProjectile();
}
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if (hitInfo.collider != null)
{
if (hitInfo.collider.CompareTag("Player"))
{
hitInfo.collider.GetComponent<Player>().TakeBulDamage();
}
if (hitInfo.collider.CompareTag("Ground"))
{
hitInfo.collider.GetComponent<GroundSFX>().PlaySound = true;
}
DestroyProjectile();
}
transform.Translate(Vector2.up * speed * Time.deltaTime);
}
void DestroyProjectile()
{
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}