My Big Boy enemy dissapears behind blue backgound

My new Enemy ‘Big Boy’ with a ‘Z’ of ‘0’ disappears behind the blue background when the 'Game" scene starts. My player Dies when hit only once but Start Menu does not launch - the Enemies just keep on spawning and firing lasers.

Any help would be much appreciated. I will provide any other necessary info if needed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // configuration parameters

    [Header("Player")]
    [SerializeField] float moveSpeed = 12f;
    [SerializeField] float padding = 1f;
    [SerializeField] int health = 300;
    [SerializeField] AudioClip deathSound;
    [SerializeField] [Range(0, 1)] float deathSoundVolume = 0.7f;
    [SerializeField] AudioClip shootSound;
    [SerializeField] [Range(0, 1)] float shootSoundVolume = 0.25f;

    [Header("Projectile")]
    [SerializeField] GameObject laserPrefab;
    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float projectileFiringPeriod = 0.1f;

    Coroutine firingCoroutine;

    float xMin;
    float xMax;
    float yMin;
    float yMax;


    // Start is called before the first frame update
    void Start()
    {
        SetUpMoveBoundaries();
    }

    // Update is called once per frame
    void Update()
    {
       Move();
       Fire();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        if(!damageDealer) { return; }
        ProcessHit(damageDealer);
    }

    private void ProcessHit(DamageDealer damageDealer)
    {
        health -= damageDealer.GetDamage();
        damageDealer.Hit();
        if (health <= 0 )
        {
            Die();
        }
    }

    private void Die()
    {
        FindObjectOfType<Level>().LoadGameOver();
        Destroy(gameObject);
        AudioSource.PlayClipAtPoint(deathSound, Camera.main.transform.position, deathSoundVolume);
    }

    public int GetHealth()
    {
        return health;
    }

    private void Fire()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            firingCoroutine = StartCoroutine(FireContinuously()); 
        }
        if (Input.GetButtonUp("Fire1"))
        {
            StopCoroutine(firingCoroutine);
        }
    }

    IEnumerator FireContinuously()
    {
        while (true)
        {
            GameObject laser = Instantiate(
                    laserPrefab,
                    transform.position,
                    Quaternion.identity) as GameObject;
            laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
            AudioSource.PlayClipAtPoint(shootSound, Camera.main.transform.position, shootSoundVolume);
            yield return new WaitForSeconds(projectileFiringPeriod);
        }
    }
   
    private void Move()
    {
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

        var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);    
        var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);

        transform.position = new Vector2(newXPos, newYPos);
    }

    private void SetUpMoveBoundaries()
    {
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
    }

    
}
type or paste code here

Hi John,

Happy new year, and welcome to our community! :slight_smile:

Is the z-position of the background set to 10, the z-position of the camera to -10 and the z-position of the other game objects to 0?


See also:

I checked all three enemy types ‘z’ position and all were at 0. The background and camera were set correctly as you outlined in your reply.

I deleted and re-did the Path (2) and all is well with Big Boy now.

But my player stills Dies when hit only once and the Start Menu does not launch - the Enemies just keep on spawning and firing lasers.

Could there be something wrong with my Player health?

Thanks, and Happy New Year and it’s great to be here!

Yes, that could well be the case.

Add a few Debug.Logs to your code to see what’s going on during runtime. For example, log the player health value into your console at start, a message which gets logged into your console when the player gets hit, and a message when the player “dies”. Log Time.frameCount into your console along with it.

other.gameObject.name could also be interesting. Maybe the player gets hit by multiple game object.

Regarding Start Menu, are there any error messages in your console when that scene is supposed to get loaded?

How are you getting on with this, @johnswan53?

Privacy & Terms