and since my Arrow from the Asset Pack uses 2 Materials
…I needed to have a different approach.
(after many hours of failed attempts)
The Weapon is Instantiating Projectiles
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class TargetLocator : MonoBehaviour
{
[Header("Weapon and Projectile")]
[SerializeField] private Transform weapon;
[SerializeField] private Transform projectileSpawnPoint;
[SerializeField] private float weaponFireRate;
[SerializeField] private Projectile projectile;
private Transform target;
private float fireRateTimer;
private void Start() {
target = FindObjectOfType<EnemyMover>().transform;
fireRateTimer = weaponFireRate;
}
private void Update() {
AimWeapon();
CheckFireRate();
}
private void AimWeapon() {
weapon.LookAt(target);
}
private void CheckFireRate() {
// if the arrow is available
if (fireRateTimer < weaponFireRate) {
fireRateTimer += Time.deltaTime;
} else {
Shoot();
fireRateTimer = 0f;
}
}
private void Shoot() {
Instantiate(projectile.gameObject, projectileSpawnPoint.position, projectileSpawnPoint.rotation);
}
}
and the Projectiles have Translation on them
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour
{
[SerializeField] private float projectileSpeed;
private void Update() {
Fly();
}
private void Fly() {
transform.Translate(Vector3.forward * projectileSpeed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other) {
Destroy(gameObject);
}
}
Works now <3