예제 #1
0
    public override void Update()
    {
        // Runs the base move speed
        base.Update();
        // Sets movement
        float horizontal = 0f;
        float vertical   = 0f;

        //  If game is not paused
        if (PauseMenu.Singleton.GamePaused == false)
        {
            //Reads the horizonal position of the player model
            horizontal = Input.GetAxis("Horizontal");
            // Reads the vertical position of the player model
            vertical = Input.GetAxis("Vertical");
        }
        // Increases input of direction to 3 to help animator understand what animation to play
        horizontal *= Speed;
        vertical   *= Speed;
        // Sets the player's movement to the horizontal and vertical inputs
        SetMovement(horizontal, vertical);

        // Creates an imaginary plane to register the mouse
        Plane plane = new Plane(Vector3.up, transform.position);
        Ray   ray   = Camera.main.ScreenPointToRay(Input.mousePosition);
        float distance;

        // Fires the ray at the plane
        // Ray starts at camera and points to the world
        if (plane.Raycast(ray, out distance))
        {
            // Get the position where the mouse landed
            Vector3 pointToLookAt = ray.GetPoint(distance);
            if (PauseMenu.Singleton.GamePaused == false)
            {
                // Look at where mouse is
                transform.LookAt(pointToLookAt);
            }
        }

        if (PauseMenu.Singleton.GamePaused == false)
        {
            // Allows the player to jump
            if (Input.GetKeyDown(KeyCode.Space) && OnGround)
            {
                // Plays rifle jump animation
                animator.Play("Rifle Jump");
                // Registers that the player is not touching the ground
                OnGround = false;
                // Disable root motion to make sure it doesn't interfere with player movement
                animator.applyRootMotion = false;
                rigidbody.AddForce(jumpForce, ForceMode.Impulse);
            }

            if (Input.GetMouseButton(0))
            {
                // Create a ray that starts at the mouse position
                Ray gunRay = Camera.main.ScreenPointToRay(Input.mousePosition);
                // Stores the results of the raycast
                RaycastHit hit;
                // Fires the ray at the world
                // Ray starts at camera and points to the world
                if (Physics.Raycast(gunRay, out hit))
                {
                    // Shoot weapon
                    weaponUser.ShootWeapon(hit.point);
                }
            }
        }
    }
예제 #2
0
파일: Enemy.cs 프로젝트: RzrCor/GPE340
    public override void Update()
    {
        // Runs the base movement of the character
        base.Update();
        // Whether the enemy is aiming towards a pickup or not
        bool aimingTowardsPickup = false;

        if (weaponUser.CurrentlyEquippedWeapon == null)
        {
            // Loop over all pickups in the game
            foreach (var pickup in allPickups)
            {
                if (pickup != null)
                {
                    //Get the weapon prefab that the pickup will give to the enemy
                    var weapon = pickup.weapon.GetComponent <Weapon>();
                    // Check if the weapon is a sniper rifle
                    if (weapon is SniperRifle)
                    {
                        // If the navagent on the enemy is enabled
                        if (navAgent.enabled)
                        {
                            if (PauseMenu.Singleton.GamePaused == false)
                            {
                                //
                                aimingTowardsPickup = true;
                                //Make the nav mesh move to the sniper rifle
                                navAgent.SetDestination(pickup.transform.position);
                                break;
                            }
                            else
                            {
                                // Make the enemy target itself so he doesn't move
                                navAgent.SetDestination(transform.position);
                                break;
                            }
                        }
                    }
                }
            }
        }
        // Check if there is a player to target
        if (targetPlayer != null)
        {
            if (navAgent.enabled)
            {
                if (PauseMenu.Singleton.GamePaused == false)
                {
                    if (aimingTowardsPickup == false)
                    {
                        //Make the nav mesh move to the target
                        navAgent.SetDestination(targetPlayer.transform.position);
                    }
                }
                else
                {
                    // Make the enemy target itself so he doesn't move
                    navAgent.SetDestination(transform.position);
                }
            }
        }

        // Check if there is a player to target
        if (targetPlayer != null)
        {
            if (PauseMenu.Singleton.GamePaused == false)
            {
                if (health.health > 0)
                {
                    // Causes enemy to look at player
                    transform.LookAt(targetPlayer.transform.position);
                    // Lock the x rotation so the enemy does not look up or down
                    transform.eulerAngles = new Vector3(0f, transform.eulerAngles.y, transform.eulerAngles.z);
                }
                //Get the velocity the nav mesh wants to move at
                Vector3 movementVector = navAgent.desiredVelocity;

                //Normalize the vector to a length of 1
                movementVector.Normalize();

                //Convert it from local-space coordinates to world-space coordinates so the animator can move in the correct direction


                movementVector.x *= Speed;
                movementVector.z *= Speed;
                // Sets the enemies movement to the navagents's desired velocity
                SetMovement(movementVector.x, movementVector.z);
                // If there is a weapon equipped
                if (weaponUser.CurrentlyEquippedWeapon != null)
                {
                    // Shoot weapon towards target player
                    weaponUser.ShootWeapon(targetPlayer.transform.position);
                }
            }
            else
            {
                // Sets the enemies movement to the navagents's desired velocity
                SetMovement(0, 0);
            }
        }
    }