Hi,
I am not sure why and if this happens for everyone, but watching the arrow frame by frame, on the first frame the arrow is spawn, it is not aiming at the target and its rotation is identity and it is then facing the wrong direction.
So, I add a little delay in the Projectile scritp to wait until the end of the frame to make the arrow visible, it is working fine, but my question is: is this a good idea or is there a better way?
using RPG.Core;
using System.Collections;
using UnityEngine;
namespace RPG.Combat
{
public class Projectile : MonoBehaviour
{
[SerializeField] private float speed = 1.0f;
[SerializeField] private GameObject arrowModel = null;
private Health target = null;
private void Awake()
{
if (arrowModel != null)
{
arrowModel.SetActive(false);
}
}
private void Start()
{
if (arrowModel != null)
{
StartCoroutine(DelayArrowModel());
}
}
private IEnumerator DelayArrowModel()
{
yield return new WaitForEndOfFrame();
arrowModel.SetActive(true);
}
private void Update()
{
if (target == null) return;
transform.LookAt(GetAimLocation());
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
public void SetTarget(Health target)
{
this.target = target;
}
private Vector3 GetAimLocation()
{
CapsuleCollider targetCapsule = target.GetComponent<CapsuleCollider>();
if (targetCapsule == null)
{
return target.transform.position;
}
return target.transform.position + Vector3.up * (targetCapsule.height / 2);
}
} // class Projectile
}