Particle effect won't appear in the correct position no matter what
I'm trying to make a gun and the sparks effect wont appear in the right location, I have a similar script for another game (its 2d tho) and the effects always appear in the right direction without any code attached to the effects itself. Here's the code that loads the bullet prefab: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GunBehavior : MonoBehaviour
{
public Transform bulletOrientation;
public bool automatic;
public int bulletsPerFire;
public float damage;
public float headshotDamage;
public string projectile;
public int magSize;
public int magAmmo;
public int reserve;
private GameObject bulletPrefab;
// Start is called before the first frame update
void Start()
{
bulletPrefab = Resources.Load(projectile) as GameObject;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
var raycast = Physics.Raycast(bulletOrientation.position, bulletOrientation.forward, out hit, 420);
if (raycast)
{
if (hit.rigidbody != null)
{
hit.rigidbody.AddForceAtPosition(bulletOrientation.forward, hit.point);
}
}
GameObject bullet = Instantiate(bulletPrefab, null);
bullet.transform.position = hit.point;
//print(hit.point);
}
}
}
And here's the script I attached to the bullet effects (inside a prefab) using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour
{
// Start is called before the first frame update
void Awake()
{
transform.position = transform.parent.transform.position;
gameObject.GetComponent<ParticleSystem>().Emit(18);
}
}
I spent quite a lot of hours googling but I didn't find anything that helped, I can't figure out whats wrong with it, I even copied a bit of code from my other game but the effect only appears in the same position every time!!!! sorry lol I'm kinda mad