Redesigning the Clear Area System

I think Ben made the ClearArea script much too complicated (minute 2:00). I decided to tackle that issue early on and arrived at a simpler solution… for me, anyway.

I initially designed a terrain with a single possible landing area, to make the goal clearer and the arrival much more dramatic, since I designed a natural stage for it.

When I started having issues with the box collider processing collisions inconveniently, I decided to play around a bit and refactored a lesson or two beforehand. My solution works if you want to have a specific number of landing points as well as spawn points.

What I ended up doing was:
1.1 I created an empty object at the landing point.
1.2 I gave the object a box collider big enough to collide with the Clear Area collider.
1.3 I gave the object an empty script called “LandingPoint”.

2.1 I refactored the ClearArea script thus:

public class ClearArea : MonoBehaviour
{
private LandingPoint landingPoint ;

void Start ()
{
landingPoint = GameObject.FindObjectOfType();
}

void OnTriggerStay(Collider collider)
{
if (collider.GetComponent () == landingPoint)
{
SendMessageUpwards (“OnFindClearArea”) ;
}
}
}

I think this could be further refactored by simply implementing this into the Player script itself, since it would be triggered by the player’s collider entering the Landing Point collider. You just have to make the Landing Point’s collider as big as a tennis court.

If you’re using more than one landing point, use tags and get rid of the .GetComponentsOfType<>() song and dance. Using tags may also fix that nasty “if” statement… I know it makes it sound like I’m assuming all colliders have a LandingPoint. It worked for me at the time, so I didn’t question it until five seconds ago.

Hello

If you have more than one possible landing zone you will also need to save/share the position of the specific one you triggered so the copter knows which destination to move towards.

I’ve customized my game so much that I can’t really say the best way to do this with the “base” code. My copter script has a SetDestination(Transform destination) as a method I call to tell the copter where to go.

Geoff

I imagine one could start by using something along the lines of GameObject.position where the GameObject is, of course, the position of the landing zone in question?

Yes you would need to send the destination position to the helicopter script so that it has a target position to “fly” towards.

G

That’s a great idea what you did. Yet, it might be a good opportunity to mention that the if(collider.GetComponent<>()) method of identifying colliders is in most occasions not the best. Even though GetComponent() is (only) a bit faster than collider.CompareTag() when the component is found it’s about 5 times slower when the function returns null (i.e. the component isn’t found). It’s absolutely negligible in cases where you expect a trigger/collision only once in a while, but as we’re speaking of OnTriggerStay() and a lot of trees/terrain to trigger it, that’s a performance hindrance. I am totally sure it won’t be perceived in our little game here, but it’s a good practice to acknowledge and note that. So, use tags (if possible) and CompareTag() (which is always faster than the gameobject.tag property).
Second point, is that all my explanation won’t matter in our case if you put the script (and trigger checking) on the LandingPoint - as it will only trigger meeting the player (and maybe the occasional zombie), and never with terrain and trees. So anyway, it might be a better option to check for triggers from the landingPoint itself.
Third and last thing I thought of, is that if there’s only a single landingPoint in such a huge area, it might be a lot of frustrating exploration for the player - remember that until the flare is shot, there’re no zombies and so no action. Your idea of several spots would diminish that endless exploration probability.
I think I am going to adopt your idea, whatsoever.

1 Like

Thanks a lot for those pointers! I think they will come in handy in my next game.
I understand your concern about there being little action and confused players looking for a unique landing area.

In my version I created a terrain with a visible path (a HUD arrow, a beacon or signs on the terrain may help too). Also, as soon as the player spawns, the helicopter landing area (located on very high ground) is quite visible because dawn breaks at that exact point, so as to draw players’ attention.

Zombies just start pouring in as soon as the game starts, so you have to go through them before reaching the landing point. When you call the helicopter with the beacon, even MORE zombies swarm you, so you have to survive somehow.

Wow, that’s a lot of things going on. I want to see this game - did you upload it to GameBucket.io?

Nah, I left it halfway through as I engaged in a Second Life project.
At this stage is basically more of a proof of concept with ambitious plans rather than an actual game. X-D

Urrgh… Ben I just did the first part of this refactoring unit and got an error saying that the code is using deprecated method? Also a prompt to update API. Not sure what to do as the project wont run anymore?

Assets/Scripts/ClearArea.cs(22,7): error CS0619: UnityEngine.Component.collider' is obsolete:Property collider has been deprecated. Use GetComponent() instead. (UnityUpgradable)’

(When I say the first part I just mean the clear area refactoring at the start and also chose the player tag.)

The error is saying that you should use GetComponent<Collider>() instead of Component.collider.

Yeah but is it as simple as literally just swapping that or will there be other dependencies? I’m a noob code wise been learning through the course so just felt like a point to freeze and ask for clarification rather than guess and brick the project?

Ps thanks for reply

Yes, it is just a different and better way to access the same component :slight_smile: it is just an syntax change.

No worries, asking in the forum is a good habit since other people with the same doubt may benefit from it too.

When you are a noob it’s a little confusing because the way it phrases it in the error is different to what I have written. So little bit tricky working out what the syntax should be.

And yeah thanks, my next thing to find on here is a solution to the fact that the player controller collider appears to be preventing the flare on the sun displaying when im in game mode lol

Privacy & Terms