[UNITY 5 FPS Tutorials] GTGD S3 - Advanced First Person Shooter

Oh wow…I cannot believe I missed that after going through it multiple times!! haha

Thanks for taking the time to read through and spotting the mistake! Really appreciate it!!

On to chapter 7!!

:slight_smile: Thanks!

Hello,
I am on ItemPickup [58] and everything was working fine until i was told to move these outside of the If statement.

public void CallEventObjectThrow()
{
if(EventObjectThrow != null)
{
EventObjectThrow();
}
playerMaster.CallEventHandsEmpty();
playerMaster.CallEventInventoryChanged();
}

public void CallEventObjectPickup()
{
if(EventObjectPickup != null)
{
EventObjectPickup();
}
playerMaster.CallEventInventoryChanged();
}

When i do this, i can no longer throw my object, it just lands on the floor in front of me, i can’t pick it back up, the inventory doesn’t update and i get no errors. I have been looking over my scripts for the past few days and cant seem to find the problem.

Just to clarify, i have the cube as item in both tag and layer.
I have the character controller set to player in both tag and layer.
I have the inputs set up correctly

Please help, i am going to go insane in a few days :smile:

You also need to put SetInitialReferences in the Start method

1 Like

Definite A, nice work and all objectives completed!
That’s a lot of evil boxes to destroy :slight_smile:

1 Like

I’m sorry but I don’t understand what you mean. In the “Item_Master” script, there is no Start method, there is only an OnEnable method which I already have called the “SetInitialReferences” method. There is one in the item throw script but I have already called the “SetInitialReferences” method. If you need any further information, I am more than willing to give it to you. I do feel a bit bad though cos I don’t want to be wasting/bothering you.

Hi :slight_smile: You need to add a start method into your item_master script, just add it underneath the on enable method and add it like this! :slight_smile:

    void Start()
    {
        SetInitialReferences ();
    }

I also have it commented out in my OnEnable method, I’m pretty sure he does that at some point in one of his videos, can’t remember which one!

Hope this helps! :slight_smile:

I find it amazing how GTGD immediately knew what the problem was only with a tiny amount of information…and how I went on to still not understand. Just wanted to thank M10Pearson(as I see you went through what I went through) and GTGD! its not programming if you don’t make mistakes :smile:

True that! My programming teacher recently sat at home for like 8 hours reading through code to find out what the problem was trying all sorts of different methods of programming it all to finally find out that it was literally just one letter that was the problem with capitalisation! haha everybody makes the mistakes and sometimes can’t spot it for hours :stuck_out_tongue:

I’m now done with Chapter 1 and ready to turn in my homework :smile: First of all, excellent tutorial so far. Easy to follow and very clear. I kept it pretty simple so that I can move on to the next chapter but it still took most of the day.

Game build link: https://drive.google.com/file/d/0B5qDW7I_vHTqNXRCQWNMUHRER2s/view?usp=sharing

A+, very nice work and it’s a bit of a platform game too!

Thanks to you :slight_smile: still working on it and im working hard to get a complete game so i can release it to the public

:slight_smile: thanks again

WOW that is awesome!
Keep at it you’ve got a really good looking project there. Lol I found it funny how you got taken down by some primitive barbarians :stuck_out_tongue:

Hi!

I’ve commented as best I can in my code, on what I understand ect…

However could you please explain why and how the set initial references method is used ect?

Thanks

Mike!

I was doing the enemy chapter, when I noticed that the enemy chases you even though you aren’t in his field of view. So I’ve decided to implement a proper detection that takes into account his fov and I’ve added another radius called stealth radius where he can “hear” you if you are behind and very close.

It works pretty well for me :stuck_out_tongue:

//Add this variables at the beginning
public float stealthRadius = 15f;
public float fov = 75f; //in degrees

//Put this inside the SetInitialReferences
fov = Mathf.Cos(fov * Mathf.Deg2Rad);

//Modify the CanPotentialTargetBeSeen
bool CanPotentialTargetBeSeen(Transform target)
{
    if (Physics.Linecast(head.position, target.position, out hit, sightLayer))
    {
        if(hit.transform == target &&
            (Vector3.Dot(head.forward, (target.position - head.position).normalized) >= fov ||
            Vector3.Distance(head.position, target.position) < stealthRadius))
        {
            enemyMaster.CallOnTargetSet(target);
            return true;
         }
        else
        {
            enemyMaster.CallOnLostTarget();
            return false;
        }
    }
    else
    {
        enemyMaster.CallOnLostTarget();
    }
    return false;
}

PD:
Something I’ve forgot to mention is that the Bip001 Head object is looking down, so the script won’t work properly. To fix this you need to create an empty object inside (called Head), rotate it so that the z axis is looking forward, and assign it to the head variable. Also, I would recommend to change the Update to LateUpdate so that the head doesn’t flicker.

I just made that up so that I can run instructions that I really need to run before any of the other instructions in that script are executed

Very nice mod there :), added to the list of contributed code!

1 Like

Greetings. I have completed “most” of the items in chapter 1. I did, however, get a bit distracted making some basic models, sound effects, and particle effects.

3 Likes

LOL, that was almost disturbing
A for turning Chapter 1 into a sci-fi horror scene!

Still grinding away at these tutorials. I really like how it’s broken up into small & digestible chunks. Really gives me a chance to let the new stuff sink in as well as give me lots of opportunities to get distracted and wander off with silly things. For example, after completing #84 Enemy Collision Field, I decided to play around with destroying the gameObject off screen. What do you think of this?:

// in Enemy_Health.cs
// Added the following variables at the top
public bool despawnOffCamera;    // bool in case I just want the original destroy in 10 to 20 seconds
 public Camera playerCam;           // drop in FirstPersonCharacter camera to check the viewport
 public float giveUpTime = 30.0f;    // time to wait before giving up destroying it anyway to avoid clutter


// Altered existing function
     void DeductHealth(int healthChange) {
            enemyHealth -= healthChange;

            if(enemyHealth <= 0) {
                enemyHealth = 0;

                // Should this be copied and pasted before each destroy?
                enemyMaster.CallEventEnemyDie(); 
                //  I haven't noticed any issues or got any warnings in Unity so I left it as is.
 
                if (despawnOffCamera) {
                    // workout how long to wait before giving up then away we go
                    giveUpTime += Time.time;
                    StartCoroutine(CheckIfVisible());
                } else {
                    Destroy(gameObject, Random.Range(10, 20));
                }
            }
        }


// Added these functions
        IEnumerator CheckIfVisible() {
            while (true) {
                AmIVisible();
                yield return new WaitForSeconds(2f);
            }
        }

        void AmIVisible() {
            if(playerCam != null) {
                Vector3 testView = playerCam.WorldToViewportPoint(transform.position);
                
               // Any value less than 0, the transform is off screen
               // I wound up using -2 to make sure the majority of the body is gone
               // While testing it, the numbers were confusing and these just seem to work good enough.
                if(testView.x < -2 || testView.y < -2 || testView.z < -2 || Time.time >= giveUpTime) {
                    StopAllCoroutines();
                    Destroy(gameObject);
                } else {
                }
            } else {
                // Incase I forget to set which camera to use, the gameObject is immediately destroyed
                // the sudden disappearance of the enemy is a good reminder to do something about it.
                StopAllCoroutines();
                Destroy(gameObject);
            }
        }

I found this handy little code to draw the enemy FOV in the #scene view to help with debugging and tweaking values. I altered the variables to match the existing code in Enemy_Detect.cs (obviously with the FOV edits too) so that it can be copied and pasted in. I also added 1 to the y position because sometimes it got lost in the floor.
http://answers.unity3d.com/answers/21180/view.html

       float  halfFOV;

halfFOV = fov / 2.0f; // Drop this in InitialReferences somewhere before the FOV variable gets converted otherwise you just get a straight line.

       void OnDrawGizmosSelected() {
            Quaternion leftRayRotation = Quaternion.AngleAxis(-halfFOV, Vector3.up);
            Quaternion rightRayRotation = Quaternion.AngleAxis(halfFOV, Vector3.up);
            Vector3 leftRayDirection = leftRayRotation * transform.forward;
            Vector3 rightRayDirection = rightRayRotation * transform.forward;
            Gizmos.DrawRay(transform.position + new Vector3(0, 1, 0), leftRayDirection * detectRadius);
            Gizmos.DrawRay(transform.position + new Vector3(0, 1, 0), rightRayDirection * detectRadius);
        }

edit: This function does not need to be called, it is built in to Unity.