2D Top Down | Instantiate on player position and stop moving instantly
So far the blood splatters appear from an array perfectly. The problem is when i instantiate the blood splatters they follow the player, when i just need them to spawn on the positions he was hit with a bullet until it fades away/deletes etc.
I'm pretty decent at piecing c# together now so if anyone can share some knowledge on my situation I'd appreciate it.
Answer by lbryan212 · Feb 07, 2019 at 08:35 PM
@xxmariofer any thoughts?
hello, i cant really tell you much without checking your code, share the code of blood splatters and the code in which you instantiate it please.
@xxmariofer I keep a ref to my bloodSplat prefab on my health script. When a bullet hits a player, the bullet calls the spawnblood method from the health script. I'm just trying to figure out the best way to spawn the bloodprefab only on the vectors where the player was hit.
Here's the bullet:
private void OnTriggerEnter2D(Collider2D other)
{
Health health = other.gameObject.GetComponent<Health>();
Name name = other.gameObject.GetComponent<Name>();
if (health.isAlive)
{
if (bulletDmg < health.cur_health)
{
other.gameObject.GetComponent<Health>().TakeDamage(bulletDmg);
// Debug.Log("Bullet detected");
Destroy(gameObject);
health.lastPosition = transform.position;
health.blood = true;
health.SpawnBlood();
Here's the health script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Health : $$anonymous$$onoBehaviour {
public Game$$anonymous$$anager game$$anonymous$$anager;
public Vector3 bloodSpot = Vector3.zero;
public string playerName;
public string killedBy;
public float max_health = 100f;
public float cur_health = 0f;
public bool isAlive;
public bool blood;
public bloodHolder bHolder;
public Transform bloodLocation;
public void Location()
{
}
public void SpawnBlood()
{
bHolder.SetSprite();
bloodHolder blood = (bloodHolder)Instantiate(bHolder, bloodLocation);
}
// Use this for initialization
void Start () {
cur_health = max_health;
isAlive = true;
}
public void TakeDamage(float amount)
{
if (cur_health <= 0)
return;
cur_health -= amount;
}
// Update is called once per frame
void Update () {
// healthBar.value = cur_health;
if(cur_health <= 0)
{
isAlive = false;
cur_health = 0;
game$$anonymous$$anager.last$$anonymous$$illed = playerName;
}
else
{
isAlive = true;
}
}
}
hello, where is the var lastPosition of the Health class? and whats the transform bloodlocation? you want the object to spawn at bloodlocation but dont have his movement you should change that line to this,
public void SpawnBlood()
{
bHolder.SetSprite();
bloodHolder blood = (bloodHolder)Instantiate(bHolder);
blood.transform.position = bloodLocation.position;
}