Collisions & Communication

Karissa Huynh
2 min readDec 13, 2020

--

Kaboom! What game logic do we use when our game objects collide with one another? Well first things first in our Unity game, we need to

1st: make sure that the certain objects have the “is Trigger” activated — to ensure that the object through objects and not bounce off”

2nd: We need to determine which objects have the Rigidbody Component (physics to an object). In my game, we ensure that enemy and laser objects have the ‘is Trigger’ on and Rigidbody Component.

To set up Collision in our script we need to ensure that we have tags on our objects. Tags are used to define objects, which makes it easier to detect the object. (ex. other.tag = “Player”). We also use the method OnTriggerEnter() to describe what will happen when our objects collided.

Funniest thing I learned today? Script Communication!

Script communication is when Script 1 calls a method in Script 2.

Essentially we cannot directly access a component in another script. But we still have a way to call a certain method. I learned that in unity, we can only access the root component, which is the “transform”. We can also get a component by using the method GetComponent.

In my game, I wanted to the Damage method in the Player script from my Enemies script. To do this we used:

other.transform.GetComponent<Player>().Damage();

other holds the ‘Player’ tag that we previously talked about. transform us tge root object(which we use to get access to the other object). GetComponent is used to access a different component. <Player> is the component we want access. Damage is the method we want to execute.

I have coded before but never fully understand what I was doing. Now that I am doing this again, I really enjoy the easy break down that GameDev gives ,which allows me to fully understand what I am doing. I’m starting to enjoy coding again and I can’t wait for the next lesson!

--

--

No responses yet