Code Snippet: Player Controller

Today I’m going to tell you about the hands down most important code in any program. That would be the code to control the player. Here is a screenshot of what it looks like:

The name of this code is PlayerController. I need to make sure wherever this is mentioned it is constant, or Unity will try to pull whatever is listed and won’t find it.

The FixedUpdate section, this what updates its position on the Horizontal axis and the Vertical axis. So float moveHorizontal(or Vertical) will move its x and y position on the axis’. Vector 3 is the X, Y, and Z axis. The rb.AddForce is what allows it to physically move. The force is what allows it to roll, and the force is just movement * speed.

At the bottom the OnTriggerEnter section is what adds a reaction when your player collides with the gameObject with the Is Trigger section checked off in the Box Collider. Debug.Log is the message that pops up in game when you collide with a trigger. In my case, its “Oh My Gawd You Hit The Trigger Collider”.