I’m making an FPS game with physical bullets and I wanted to add a blood spray where you hit an enemy. The effect works fine and it spawns upon hitting an enemy, the issue is it doesnt spawn where I hit the enemy, instead it spawns a few feet away from the enemy in the air. I’m not quiet sure how to fix this so any help is greatly appreciated. Attached below is the bullet script that causes the effect to proc.
if (objectWeHit.gameObject.CompareTag("Enemy"))
{
print("hit a Enemy");
objectWeHit.gameObject.GetComponent<Enemy>().TakeDamage(bulletDamage);
CreateBloodSprayEffect(objectWeHit);
Destroy(gameObject);
}
}
private void CreateBloodSprayEffect(Collision objectWeHit)
{
ContactPoint contact = objectWeHit.contacts[0];
GameObject bloodSprayPrefab = Instantiate(
GlobalReferences.Instance.bloodSprayEffect,
contact.point,
Quaternion.LookRotation(contact.normal)
);
bloodSprayPrefab.transform.SetParent(objectWeHit.gameObject.transform);
}`
Also attached is the global References singleton script.
public class GlobalReferences : MonoBehaviour
{
public static GlobalReferences Instance {get; set; }
public GameObject bloodSprayEffect;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}
}