- Home /
FPS Camera
i have been struggling with getting a First Person camera set up, i have a capsule and the main camera locked up. my movement script is on the capsule and you can see it down below.
thanks already`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MovementScript : MonoBehaviour {
public float speed = 5F;
// Use this for initialization
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update ()
{
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translation);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
} `
Answer by NathanGG · May 04, 2018 at 02:44 PM
I used to have the same problem, but here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour {
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
GameObject character;
// Use this for initialization
void Start ()
{
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update ()
{
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
mouseLook.y = Mathf.Clamp (mouseLook.y, -90f, 90f);
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
}
}
It's actually very simple!
Oh, and put it on the camera, and attach the camera to the capsule (player) and it should work.
Answer by Kciwsolb · May 04, 2018 at 03:24 PM
If you want to learn, this site seems to walk you through making one. I recommend you go through that. Then you will actually understand how what you are using works.
Otherwise there are so many options available if you just do this. Do the same thing if you want a camera look script.