Hey guys! Any suggestions for a solution is more than welcome.
My issue is fairly simple but I’m uploading everything connected to the issue just in case. I also hope I’m making sense here (english is not my first language).
- I’m instantiating a prefab (attached “hierarchy cutout”)
- the RayCaster casts a ray to find any enemy directly in front of the player (pink capsule).
- The RayCaster then moves the DetonationObject to the raycast hitPoint (or 30 points along the z-axis.
- ParticleEmitter is called by the DetonationObject in “InitializeParticleSystem”.
For some doggone reason the RayCaster refuse to cast straight ahead (Vector3.forward) and tilts the ray. It is essential here that the tilt perfectly correlates with player position on the y-axis. Also the y-value differential between “relativeHitPos” and Player is almost exactly the same as the y-value differential between Player and origo (attached GameView2). Lastly as you can see the actual particles are rendered way above the ray.
I can’t find anything strange in the code. It looks like a scaling issue to me but all transforms are zero and the Instantiation does not assign a parent to the prefab. Can it be part of the particleSystem-component it self? I KNOW this is obvious to someone but I’m oblivious after several hours.
Instantiation of prefab “Particle1”
Instantiate(projectiles.Items[preppedWeapon], transform.position, Quaternion.identity);
RayCaster:
private void CastRay()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hitData;
if (Physics.Raycast(ray, out hitData, 30, layerMask))
{
Vector3 hitPos = hitData.collider.gameObject.transform.position;
Vector3 relativeHitPos = transform.position + hitPos;
Debug.DrawRay(transform.position, relativeHitPos, Color.blue, 2);
StartCoroutine(ActivateDetonationObject(relativeHitPos));
}
else
{
Debug.DrawRay(transform.position, new Vector3(transform.position.x,
transform.position.y, transform.position.z + 30), Color.blue, 2);
StartCoroutine(ActivateDetonationObject(new Vector3(transform.position.x,
transform.position.y, transform.position.z + 30)));
}
}
private IEnumerator ActivateDetonationObject(Vector3 moveTo)
{
detonationObject.transform.Translate(moveTo);
detonationObject.SetActive(true);
yield return new WaitForFixedUpdate();
//Debug option.
//yield return new WaitForSeconds(2);
detonationObject.SetActive(false);
detonationObject.transform.Translate(transform.position);
}
ParticleEmitter:
public void InitiateParticleSystem(Vector3 endPos)
{
startPosition = transform.position;
endPosition = endPos;
Vector3 particlePosition = (endPosition - startPosition) / 2 + startPosition;
float distance = Vector3.Distance(endPosition, startPosition) / 2;
int numParticles = (int)distance * 100;
particleSystem.transform.LookAt(endPosition);
ParticleSystem.ShapeModule sm = particleSystem.shape;
sm.position = particlePosition;
sm.radius = distance;
ParticleSystem.EmissionModule em = particleSystem.emission;
em.rateOverTime = numParticles;
particleSystem.Play();
}
