biyaniM / ALICE-IN-WONDERLAND-professional-nappers

Single Player, 3D Platformer movement-based game with Color and Level Mechanics with WebGL Builds
6 stars 1 forks source link

enemy player shooting track fix #47

Closed biyaniM closed 2 years ago

biyaniM commented 2 years ago

Fixed the enemy movement and shooting track. Fix was in three steps -

  1. Added a variable to determine the "UpperChest" of the player character. Did this because the default Skeleton placeholder is at the feet of the character. CharacterSkeletonDefault However, the upper chest is located at the upper chest (🤓) SkeletonUpperChest This change was made using the following function -

    private Transform GetSkeletonPlayerTargetTransform(GameObject playerArmature, string 
    skeletonPart="Skeleton/Hips/Spine/Chest/UpperChest")
    {
        return playerArmature.transform.Find(skeletonPart);`
        }

    Calling it as follows in the Start function- playerTarget = GetSkeletonPlayerTargetTransform(player);

  2. Calculate the Difference between the player and the turret along with the relative angle of their vector positions- Vector3 playerDirection = playerTarget.transform.position - transform.position; distance = Vector3.Distance(playerTarget.transform.position, transform.position);

  3. Use the distance to trigger the enemy turret movement -

    if(fireRateDelta <= 0 && distance < turretShootRange)
        {
            //* Rotate if the player is in range for the turret
            Quaternion rotation = Quaternion.LookRotation(playerDirection);
            Quaternion current = transform.localRotation;
            transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime * turretRotationOnDetectionSpeed);`
    
            //* Angle function checks the angle between the players position vector and enemy's position vector
            if (180-Vector3.Angle(currentGun.transform.forward,playerTarget.transform.position)<=angleThreshold){
                //* If the enemy is in a certain angle threshold, only then shoot
                currentGun.Fire();
                fireRateDelta = fireRate;
            } 
        }  

    Here we use the relative angle to see if the gun of the turret can actually see the player. If it can with a threshold, only then will it shoot, or else it will shoot wherever it is looking when the player is in range.

biyaniM commented 2 years ago

Next Enhancement - Turret shoots only if it can see the player (RayCast)