Game Development Reference
In-Depth Information
4. After that, we create the
Start()
function to make sure that our character has a
rigidbody
component attached, even though we don't have it.
Tip
This setting is just for the fact that we can reuse the code for other character's set-
tings in the future.
We will freeze the
rigidbody
rotation as follows:
// Unity JavaScript user:
function Start () {
if (rigidbody) { rigidbody.freezeRotation = true; }
}
//C# user:
void Start () {
if (rigidbody) { rigidbody.freezeRotation = true; }
}
5. Next, we will create the
Update()
function. In this function, we will calculate
the rotation of each axis and assign the value to the character to make it rotate
when moving the mouse in each direction, as follows:
// Unity JavaScript user:
function Update () {
if (axes == RotationAxes.MouseXAndY) {
_rotationX += Input.GetAxis("Mouse X") *
sensitivityX;
_rotationX = Mathf.Clamp (_rotationX, minimumX,
maximumX);
_rotationY += Input.GetAxis("Mouse Y") *
sensitivityY;
_rotationY = Mathf.Clamp (_rotationY, minimumY,
maximumY);
transform.localEulerAngles = new