Graphics Reference
In-Depth Information
myTransform.position= Vector3.Lerp( myTransform.position, followTarget.
position + targetOffset, moveSpeed * Time.deltaTime );
The single line controlling our camera uses the Vector3.Lerp function to interpolate
the camera's transform's position to that of the target stored in followTarget. We add an
offset to the position (so that we end up above the target rather than inside it) and use the
moveSpeed multiplied by Time.deltaTime to interpolate independent of frame rate.
It is a very simple camera but one that works really well for basic top-down games.
Note that in the Lazer Blast Survival example game for this topic, an empty gameObject
named CamTarget is used as the camera target. It is positioned in front of the player, par-
ented to the player, so that it inherits the player's rotation. By positioning a target object
in front of the player for use with a top-down camera like this, it can help gameplay by
providing a better view ahead of the player rather than centering it on the screen.
4.7 Input Scripts
In Chapter 3, we saw that the main player structure contained a single input script
designed to take keyboard input. Adding input systems is a case of building custom input
scripts that follow a similar format, so that the existing player code need not be changed
to accommodate different controls.
It may be required to take input from other methods than the keyboard, such as a
mouse or a joystick. You can easily set up alternate controls in the Unity editor via the Edit
-> Project Settings -> Input menu.
4.7.1 Mouse Input
The Mouse_Input.cs script calculates the movement of the mouse between each frame and
uses it for input.
Below is the script:
public class Mouse_Input : BaseInputController
{
private Vector2 prevMousePos;
private Vector2 mouseDelta;
private float speedX = 0.05f;
private float speedY = 0.1f;
public void Start ()
{
prevMousePos= Input.mousePosition;
}
public override void CheckInput ()
{
// get input data from vertical and horizontal axis and
// store them internally in vert and horz so we don't
Search WWH ::




Custom Search