i have made a script for a bullet, and when it hits a bird, it instantiates a feather particle system, except the particle system’s position is miles away and i don’t know why. here’s the script: `using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float expireRate;
private float currentTimer;
public GameObject otherPlayer;
public float movSpeed;
public float damageRate;
public GameObject feathers;
//public Renderer rend;
//public Collider coll;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Vector3.left * Time.deltaTime * movSpeed);
currentTimer += 1 * Time.deltaTime;
if (currentTimer > expireRate)
Destroy(this.gameObject);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Bird")
{
//otherPlayer = col.gameObject;
Debug.Log("killed-bird");
//Destroy(col.gameObject);
//Destroy(gameObject);
col.gameObject.GetComponent<Renderer>().enabled = false;
//rend.enabled = false;
//coll.enabled = false;
Instantiate(feathers, col.transform);
}
Destroy(this.gameObject);
}
}
`
is there anyway to actually get it to instantiate at the right position?
Thanks
If you read the documentation here you will see that the Object.Instantiate method has a few overloads. You are setting its parent, not its position.
You need to pass a vector and rotation to set its position.
Here is my solution, i just wrote you a script you need to attach it to your bird, and tag your bullets with the tag “Bullet”. Please take a look try it and if you have any problem with setting it up just let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BirdHit : MonoBehaviour
{
[Header("Bird Settings:")]
public GameObject featherParticle; // Put your particle prefab here.
public AudioClip birdDieSFX; // You could put some sound FX to be played when your bird die.
private bool isHit; // Bool that will control your paticle destruction.
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Bullet"))
{
Instantiate(featherParticle, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(birdDieSFX, transform.position);
Destroy(gameObject);
isHit = true;
Debug.Log("You hit the bird!");
// This will destroy the particle clone in hierarchy.
if(isHit)
{
Destroy(featherParticle, 1.0f);
}
}
}
}
Hope this will help you out @efobrog!