Пример #1
0
    void OnTriggerEnter(Collider other)
    {
        //Check if layer is asteroid.
        if (other.gameObject.layer == 8)
        {
            Vector3 forceVec = -mBody.velocity.normalized * ContactRepelForce;
            mBody.AddForce(forceVec, ForceMode.Acceleration);
            LoseLife();
        }

        if (other.CompareTag("Shooting Asteroid"))
        {
            gameObject.SetActive(false);
            lifebar.SetLife(0);
            IsDead = true;
            //Play particle effects/sound/screenshake etc.
        }

        if (other.CompareTag("UFO"))
        {
            //destroy ufo, add score, lose life
            other.gameObject.SetActive(false);
            LoseLife();
        }
    }
 private void OnProjectHit()
 {
     health -= 1;
     if (health == 0)
     {
         Destroy(gameObject);
         GetEnemyCounter?.ReduceCountByOne();
         return;
     }
     lifebar.SetLife(health / startHealth);
 }
Пример #3
0
    void Awake()
    {
        scoreText = playerUI.GetComponentInChildren <Text>();
        lifebar   = playerUI.GetComponentInChildren <Lifebar>();

        Vector2[] meshShape =
        {
            new Vector2(0,   3),
            new Vector2(2,  -2),
            new Vector2(0,  -1),
            new Vector2(-2, -2)
        };

        // Use the triangulator to get indices for creating mesh collider
        Triangulator tr = new Triangulator(meshShape);

        int[] indices = tr.Triangulate();

        // Create the Vector3 vertices
        Vector3[] vertices = new Vector3[meshShape.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = new Vector3(meshShape[i].x, 0, meshShape[i].y);
        }

        // Create the mesh
        Mesh msh = new Mesh();

        msh.vertices  = vertices;
        msh.triangles = indices;
        msh.RecalculateNormals();
        msh.RecalculateBounds();

        //Initialise collider and renderer.
        MeshFilter filter = GetComponent <MeshFilter>();

        filter.mesh = msh;
        MeshCollider collider = GetComponent <MeshCollider>();

        collider.sharedMesh = msh;
        mBody = GetComponent <Rigidbody>();

        //Set lives count
        currentLives = MaxLives;
        lifebar.SetLife(currentLives);
        thrustIcon = transform.GetChild(1).gameObject;
        IsDead     = false;
    }
    private void Start()
    {
        lifebar = GetComponentInChildren <Lifebar>();
        lifebar.SetLife(1);
        startHealth = health;
        target      = GameObject.FindWithTag("Player");
        var animator = GetComponentInChildren <Animator>();

        animator.SetBool("Walk", true);

        NavMeshHit closestHit;

        if (NavMesh.SamplePosition(transform.position, out closestHit, 500, 1))
        {
            transform.position = closestHit.position;
            agent = GetComponent <NavMeshAgent>();
        }
    }