Hello all,
EDIT:
and I know, i misspelled the word “muzzle” :/.
am doing the Brackeys 2D Platformer tutorial and it is a bit outdated but thats fine, becouse it is a bit more challenging. Netherless I need help on one of the issues, that I cannot solve.
I do a 2D platform shooter and am adding now a flash effect to the muzzle, but can’t rotate it in the right way.
The thing is, that my character shoots along the y axis (gameObject.transform.up). I have turned the axis of the muzzle in the scene view so, that it would face the right/shooting direction.
Now my flash effect shows up in the x-axis direction and I don’t know, how to tun it.
Here is some of my code.
Character controller script for flipping the character:
if (myRigidbody2D.velocity.x < 0)
{
//transform.Rotate(0f, 180f, 0f);
transform.localScale = new Vector3(-1f, 1f, 1f);
}
else if (myRigidbody2D.velocity.x > 0)
{
//transform.Rotate(0f, 180f, 0f);
transform.localScale = new Vector3(1f, 1f, 1f);
}
Code for shooting:
IEnumerator Shoot()
{
Vector2 muzlePos = new Vector2(muzle.transform.position.x, muzle.transform.position.y);
RaycastHit2D hitInfo = Physics2D.Raycast(muzlePos, muzle.transform.up, 100f, whatToHit);
Debug.LogError("PifPaf!");
if (hitInfo)
{
EnemyBehaviour enemy = hitInfo.transform.GetComponent<EnemyBehaviour>();
if(enemy != null)
{
enemy.TakeDamage(damage);
}
Instantiate(hitEffect, hitInfo.point, Quaternion.identity);
lineRenderer.SetPosition(0, muzle.position);
lineRenderer.SetPosition(1, hitInfo.point);
}
else
{
Debug.DrawRay(muzlePos, muzle.transform.up * 100f, Color.yellow);
lineRenderer.SetPosition(0, muzle.position);
lineRenderer.SetPosition(1, muzle.transform.up * 100f);
}
lineRenderer.enabled = true;
//wait for one frame
yield return new WaitForSeconds(0.02f);
lineRenderer.enabled = false;
}
void WeaponFXEffects()
{
Transform cloneMuzleFlash = (Transform)Instantiate(MuzleFlashPrefab, muzle.position, muzle.rotation);
//cloneMuzleFlash.parent = muzle; // <- muzzle flash not appearing after this line is in the code. After erase, the flash shows only in the right direction
float size = Random.Range(1.6f, 1.9f);
cloneMuzleFlash.localScale = new Vector3(size, size / 2, size);
Destroy(cloneMuzleFlash.gameObject, 0.02f);
//Instantiate(weaponSmoke, transform.position, transform.rotation);
}