Ok hi, I came up with this: :]
var walkSpeed = 10;
var horizontalSpeed = 2.0;
var verticalSpeed = 2.0;
function Update()
{
var e:Event = Event.current;
if(e.isMouse)
{
var currentPos = e.mousePosition;
if(e.x > currentPos.x)
{
transform.rotate(horizontalSpeed,0,0);
e.x = currentPos.x;
} else if(e.x < currentPos.x)
{
transform.Rotate(0,verticalSpeed,0);
}
}
if(Input.GetButton("W"))
{
transform.position += transform.foward * walkSpeed * Time.deltaTime;
}
if(Input.GetButton("S"))
{
transform.position += -transform.foward * walkSpeed * Time.deltaTime;
}
if(Input.GetButton("A"))
{
transform.position += -transform.right * walkSpeed * Time.deltaTime;
}
if(Input.GetButton("D"))
{
transform.position += transform.right * walkSpeed * Time.deltaTime;
}
}
As you can see my wasd keys move the character. Also i do not want the camera to do a barrel roll when i look over.
1: Use the Input class. It includes several very useful functions for simplifying input calculations- for example, you can do something like this to reduce your movement section to just a few lines:
var inputVector : Vector3 =
Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(inputVector * Time.deltaTime * speed);
This will give you a simple, physics-free strafe movement in just 2 lines.
2: As for the mouselook, I find a good way of handing this is to split the camera up into a hierarchy of objects- with the base player at one end, and the camera at the other:
Player (rotates around y axis)
---> Camera (rotates around Player's x axis)
Then, do something like this:
var xSensitivity : float;
var ySensitivity : float;
var maxHeight : float;
var minHeight : float;
private var xPos : float;
private var yPos : float;
var myTrans : Transform;
var camTrans : Transform;
function Update () {
yPos = Mathf.Clamp(yPos - Input.GetAxis ("Mouse Y") * ySensitivity, minHeight, maxHeight);
xPos = xPos + Input.GetAxis("Mouse X") * xSensitivity;
myTrans.rotation = Quaternion.AngleAxis(xPos, Vector3.up);
camTrans.localRotation = Quaternion.AngleAxis(yPos, Vector3.right);
}
Assign things to the correct slots (camera in cam slot, character in myTrans slot), and it will give you a nice, limited viewport.