Using the LookAt() method to aim
In this recipe, we will use a simple but effective implementation of aiming mechanics.
Getting ready
We are going to use the same assets as in the previous recipe. Additionally, we are going to use the CharacterLookAt.cs
created in the Using Quaternion.LookRotation() and Animator.SetLookAtPosition() methods to make characters follow an object with their gaze recipe in Chapter 5, Character Actions and Expressions.
You can also use the provided example Unity project and go to the Chapter 06 Handling combat\Recipe 08 Using the LookAt method to aim
directory. You will find an Example.unity
scene there, with a Humanoid
character. Play the game to see the aiming effect.
How to do it...
To use the LookAt()
method for aiming, follow these steps:
- Import your character to Unity and place it on the
Scene
. - Follow the previous recipe to make a character walk and aim.
- Attach the
CharacterLookAt.cs
script to the character. - Create a new
Sphere
object and name itAimTarget
. - Assign...