78 lines
1.8 KiB
C#
78 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
namespace LE_LevelEditor.Example
|
|
{
|
|
public class KeyAndMouseControlsDungeon : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private float SPEED = 5f;
|
|
|
|
[SerializeField]
|
|
private float ROT_SPEED = 1f;
|
|
|
|
[SerializeField]
|
|
private Transform CAMERA_PIVOT;
|
|
|
|
private CharacterController m_character;
|
|
|
|
private Vector3 m_lastMousePos;
|
|
|
|
private int m_lastTouchFrame;
|
|
|
|
private void Start()
|
|
{
|
|
m_character = GetComponent<CharacterController>();
|
|
m_lastMousePos = Input.mousePosition;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.touchCount != 0)
|
|
{
|
|
m_lastTouchFrame = Time.frameCount;
|
|
}
|
|
Vector3 zero = Vector3.zero;
|
|
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
|
|
{
|
|
zero += Vector3.forward;
|
|
}
|
|
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
|
|
{
|
|
zero += Vector3.left;
|
|
}
|
|
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
|
|
{
|
|
zero += Vector3.back;
|
|
}
|
|
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
|
|
{
|
|
zero += Vector3.right;
|
|
}
|
|
if (Input.GetKey(KeyCode.Space))
|
|
{
|
|
zero += Vector3.up;
|
|
}
|
|
zero.Normalize();
|
|
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
|
|
{
|
|
zero *= 5f;
|
|
}
|
|
if (zero != Vector3.zero)
|
|
{
|
|
m_character.Move(Camera.main.transform.TransformDirection(zero * SPEED * Time.deltaTime));
|
|
}
|
|
if (Input.mousePosition != m_lastMousePos)
|
|
{
|
|
if (m_lastTouchFrame + 10 < Time.frameCount && Input.GetMouseButton(1))
|
|
{
|
|
Vector3 zero2 = Vector3.zero;
|
|
zero2.x = (0.5f - Input.mousePosition.y / (float)Screen.height) * 65f + 90f;
|
|
zero2.y = Input.mousePosition.x / (float)Screen.width * 600f * ROT_SPEED;
|
|
CAMERA_PIVOT.rotation = Quaternion.Euler(zero2);
|
|
}
|
|
m_lastMousePos = Input.mousePosition;
|
|
}
|
|
}
|
|
}
|
|
}
|