Пример #1
0
        public void OnCollisionEnter(Collision collision)
        {
            SimpleDamage damage = collision.collider.GetComponent <SimpleDamage>();

            if (damage != null)
            {
                damage.TakeDamage(0.6f, owner);
            }
        }
Пример #2
0
        public void Update()
        {
            if (damage.IsDead == true)
            {
                return;
            }

            float distanceToPlayer = Vector3.Distance(transform.position, playerTarget.position);

            Vector3 directionToPlayer = (playerTarget.position - transform.position);

            bool isInSight = false;

            // Check distance
            if (distanceToPlayer <= viewRange)
            {
                if (Vector3.Dot(transform.forward, directionToPlayer.normalized) > 0.6f)
                {
                    // Perform line of sight raycast
                    RaycastHit hit;
                    if (Physics.Raycast(new Ray(transform.position, directionToPlayer.normalized), out hit) == true)
                    {
                        // Check for matching hit object
                        if (hit.transform == playerTarget)
                        {
                            isInSight = true;
                        }
                    }
                }
            }



            // Check for not in sight and move to player
            if (isInSight == false)
            {
                agent.SetDestination(playerTarget.transform.position);
                agent.updateRotation = true;
            }

            // Update animator
            anim.SetBool("PlayerInSight", isInSight);
            anim.SetFloat("Speed", agent.velocity.magnitude);

            // Aim at player
            if (distanceToPlayer < shootRange)
            {
                // Set the current destination to match the current position
                agent.SetDestination(transform.position);

                if (Vector3.Dot(transform.forward, directionToPlayer.normalized) > 0.98f)
                {
                    SimpleDamage playerDamage = playerTarget.GetComponent <SimpleDamage>();

                    if (playerDamage.IsDead == false && Time.time > lastShootTime + 0.7f)
                    {
                        Instantiate(bulletPrefab, bulletSpawn.position, Quaternion.identity).Shoot(gameObject, bulletSpawn.forward);

                        // Shoot the player
                        //playerDamage.TakeDamage(0.6f, gameObject);

                        // Play muzzle flash
                        muzzleFlash.Play();

                        // Reset shoot timer
                        lastShootTime = Time.time;
                    }
                }
                else
                {
                    Vector3 lookDirection = directionToPlayer;
                    lookDirection.y = 0;

                    Quaternion look = Quaternion.LookRotation(lookDirection);

                    agent.updateRotation = false;

                    float lookRotationSpeed = agent.angularSpeed * Time.deltaTime * 0.2f;

                    if (lookRotationSpeed < 0.3f)
                    {
                        lookRotationSpeed = 0.3f;
                    }

                    if (distanceToPlayer < 4)
                    {
                        lookRotationSpeed = 1f;
                    }

                    transform.rotation = Quaternion.Slerp(transform.rotation, look, lookRotationSpeed);
                }
            }
        }