Skip to content

Instantly share code, notes, and snippets.

@IJEMIN
Last active September 19, 2020 22:22
Show Gist options
  • Save IJEMIN/b6868046085348cf22ff23bb56acdb79 to your computer and use it in GitHub Desktop.
Save IJEMIN/b6868046085348cf22ff23bb56acdb79 to your computer and use it in GitHub Desktop.
Clean FPS Character Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class FPSController : MonoBehaviour
{
private PlayerInput m_PlayerInput;
private float speed = 5.0f;
private Vector3 m_moveHorizontal;
private Vector3 m_movVertical;
private Vector3 m_velocity;
private Rigidbody m_Rigid;
private Vector3 m_rotation;
private Vector3 m_cameraRotation;
private float m_lookSensitivity = 3.0f;
private bool m_cursorIsLocked = true;
[Header("The Camera the player looks through")]
public Camera m_Camera;
// Use this for initialization
private void Awake()
{
m_Rigid = GetComponent<Rigidbody>();
m_PlayerInput = GetComponent<PlayerInput>();
}
// Update is called once per frame
public void Update()
{
//mouse movement
m_cameraRotation = new Vector3(m_PlayerInput.rotate.y, 0, 0) * m_lookSensitivity;
//apply camera rotation
if (m_Camera != null)
{
//negate this value so it rotates like a FPS not like a plane
m_Camera.transform.Rotate(-m_cameraRotation);
}
InternalLockUpdate();
}
void FixedUpdate()
{
var moveDirection = transform.right * m_PlayerInput.move.x + transform.forward * m_PlayerInput.move.y;
var velocity = moveDirection.sqrMagnitude >= 1f ? moveDirection.normalized * speed : moveDirection * speed;
m_Rigid.MovePosition(m_Rigid.position + velocity * Time.deltaTime);
m_rotation = new Vector3(0, m_PlayerInput.rotate.x, 0) * m_lookSensitivity;
if (m_rotation != Vector3.zero)
{
//rotate the camera of the player
m_Rigid.MoveRotation(m_Rigid.rotation * Quaternion.Euler(m_rotation));
}
}
//controls the locking and unlocking of the mouse
private void InternalLockUpdate()
{
if (Input.GetKeyUp(KeyCode.Escape))
{
m_cursorIsLocked = false;
}
else if (Input.GetMouseButtonUp(0))
{
m_cursorIsLocked = true;
}
if (m_cursorIsLocked)
{
UnlockCursor();
}
else if (!m_cursorIsLocked)
{
LockCursor();
}
}
private void UnlockCursor()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void LockCursor()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
using UnityEngine;
// 플레이어 캐릭터를 조작하기 위한 사용자 입력을 감지
// 감지된 입력값을 다른 컴포넌트들이 사용할 수 있도록 제공
public class PlayerInput : MonoBehaviour
{
public string moveVerticalAxisName = "Vertical"; // 앞뒤 움직임을 위한 입력축 이름
public string moveHorizontalAxisName = "Horizontal"; // 좌우 회전을 위한 입력축 이름
public string rotateHorizontalAxisName = "Mouse X";
public string rotateVerticalAxisName = "Mouse Y";
public string fireButtonName = "Fire1"; // 발사를 위한 입력 버튼 이름
public string reloadButtonName = "Submit"; // 재장전을 위한 입력 버튼 이름
public string jumpButtonName = "Jump";
// 값 할당은 내부에서만 가능
public Vector2 moveInput{get; private set;}
public float mouseX { get; private set; }
public float mouseY { get; private set; }
public bool fire { get; private set; } // 감지된 발사 입력값
public bool reload { get; private set; } // 감지된 재장전 입력값
public bool jump {get; private set;}
// 매프레임 사용자 입력을 감지
private void Update()
{
moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if(moveInput.sqrMagnitude < 1f)
{
moveInput *= 0.707f; // 0.707 = Sqrt of 2
}
else
{
moveInput = moveInput.normalized;
}
jump = Input.GetButtonDown(jumpButtonName);
// fire에 관한 입력 감지
fire = Input.GetButtonDown(fireButtonName);
// reload에 관한 입력 감지
reload = Input.GetButtonDown(reloadButtonName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment