SingleInputController
Just a simple modified version of FPSInputController.
/* One hand input controller. Uses left/right keys for rotating player
* and up/down keys for moving forward/backwards.
* */
using UnityEngine;
using System.Collections;
// Require a character controller to be attached to the same game object
[RequireComponent(typeof(CharacterMotor))]
public class SingleInputController : MonoBehaviour {
public float rotationSpeed = 3.0f;
private CharacterMotor motor;
void Awake () {
motor = GetComponent<CharacterMotor>();
}
void Update () {
// Rotate character on left/right.
transform.Rotate(0, Input.GetAxis("Horizontal") * rotationSpeed, 0);
// Walk forward/backward on up/down.
Vector3 directionVector = new Vector3(0, 0, Input.GetAxis("Vertical"));
if (directionVector != Vector3.zero) {
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
directionLength = Mathf.Min(1, directionLength);
directionLength = directionLength * directionLength;
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
}
Recent Comments