Third Person Aim in Unity

Oct 27, 2013

O.A. writes: Hi,
I am a Unity newbie and would like to pick your brain on a Unity problem I have, if it’s not too much trouble. I want to make a 3rd person game with simple movements but I can’t seem to find a simple concise and direct script to just make the arms and gun aim where I place the mouse pointer on the screen. Any advice and help will be much appreciated.

Here’s a small example demonstrating how to get the arms/gun to aim where the mouse pointer is placed on the screen. After unzipping all contents, open the included UnityThirdPersonGunExample/Assets/ThirdPersonGunExample.unity scene file. Move the mouse to aim, click to fire at the stacked boxes.

As for third person movements, the car driving covered in my 1 hour Unity crash course video (and its newer followup showing plane control and terrain) should be a workable starting point. That code could be combined with that aim script supplied above to get the third person movements and shooting together.

(For readers totally new to Unity: download the free version of Unity. The free version includes everything we need. Maybe check out that hour tutorial video to get started.)

Please note that the aimCursor GameObject in the script isn’t essential, it’s just there to show where the player is currently aiming. The object attached to that slot on the script needs to have its collision detection disabled to avoid the mouseRay hitting the aim indicator. Note too that the projectile prefab has a rigidbody component on it, which is necessary since in this script setting the velocity on the shot after it’s spawned is how the projectile’s motion is accomplished.

In a more robust implementation the projectile prefab would be added to a collision layer, and a mask could be applied to the mouse raycast to avoid the cursor/gun aiming at the player’s own projectiles. To keep this example as simple as possible though I wanted to minimize complication of project or connected object settings.

Good luck!

Learn and practice team game development with Gamkedo Club.
Membership worldwide. Professional support. Proven process.

Subscribe by e-mail to receive weekly updates with Gamkedo.Community interviews and YouTube training videos for game developers!

All contents Copyright ©2018 Chris DeLeon.

Site production by Ryan Burrell.