- Home /
First Person Shooter Help With Cam
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.
Well, for starters this is pretty far removed from any FPS controller I would have written. For example, why are you using hard-coded keys, ins$$anonymous$$d of input axes? Why aren't you using the Input class for anything? It occurs to me that your mouselook is overly simplistic and error-prone. Look through the standard assets, there's a few good things in there for all of this- have a read through them, and learn how to do things properly.
wow harsh. there is no jumping in this game so im using a strafe. I have been learning slowly. Ins$$anonymous$$d of being a critic, you should actually provide factual help.
$$anonymous$$aybe if you weren't so argumentative, your karma would be over -20. Just saying.
I'm not argumentative, there are random answer hoppers disliking stuff.
Answer by syclamoth · Apr 19, 2012 at 06:47 AM
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.
ok i get this. ut the one thing that stumps me is: heres my tree Character > main camera... now myTras would be? and Cam trans would be?
myTrans is the character, camTrans is the camera. Assign them in the editor.
Oh, make sure the camera is a transform child of the charater, it won't work otherwise.
ok i had to play with some variables. works fine. How do i lock my mouse? so i can press esc and it unlocks and vise versa
Your answer
Follow this Question
Related Questions
Can't stop camera from rotating on Z Axis 1 Answer
Rotating Player with Mouse 0 Answers
How does this first person camera script work? 0 Answers
Setting camera pitch - Never works? 0 Answers
Unity3d FPS Gun rotating on camera issue 2 Answers