Code Snippet: Player Controller

elijah richards
Aug 29, 2017 · 1 min read

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”.

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade