Skip to content

Commit

Permalink
add camera script
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Nov 29, 2023
1 parent b88d53c commit 0a9cb69
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using UnityEngine;

namespace UniRecast.Runtime
{
public static class KeyModState
{
public const int None = 0;
public const int Shift = 1;
public const int Control = 2;
}

public class UniRcCamera : MonoBehaviour
{
private float camSens = 0.25f;
private Vector3 lastMouse = new Vector3(255, 255, 255);

// input
private int _modState;

private float _moveFront;
private float _moveLeft;
private float _moveBack;
private float _moveRight;
private float _moveUp;
private float _moveDown;
private float _moveAccel;


private void Update()
{
lastMouse = Input.mousePosition - lastMouse;
lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;

UpdateKeyboard(Time.deltaTime);
UpdateCamera(Time.deltaTime);
}

private void UpdateKeyboard(float dt)
{
var tempMoveFront = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) ? 1.0f : -1f;
var tempMoveLeft = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) ? 1.0f : -1f;
var tempMoveBack = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow) ? 1.0f : -1f;
var tempMoveRight = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) ? 1.0f : -1f;
var tempMoveUp = Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.PageUp) ? 1.0f : -1f;
var tempMoveDown = Input.GetKey(KeyCode.E) || Input.GetKey(KeyCode.PageDown) ? 1.0f : -1f;
var tempMoveAccel = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) ? 1.0f : -1f;
var tempControl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);

_modState = 0;
_modState |= tempControl ? (int)KeyModState.Control : (int)KeyModState.None;
_modState |= 0 < tempMoveAccel ? (int)KeyModState.Shift : (int)KeyModState.None;

//Logger.Information($"{_modState}");
_moveFront = Mathf.Clamp(_moveFront + tempMoveFront * dt * 4.0f, 0, 1.0f);
_moveLeft = Mathf.Clamp(_moveLeft + tempMoveLeft * dt * 4.0f, 0, 1.0f);
_moveBack = Mathf.Clamp(_moveBack + tempMoveBack * dt * 4.0f, 0, 1.0f);
_moveRight = Mathf.Clamp(_moveRight + tempMoveRight * dt * 4.0f, 0, 1.0f);
_moveUp = Mathf.Clamp(_moveUp + tempMoveUp * dt * 4.0f, 0, 1.0f);
_moveDown = Mathf.Clamp(_moveDown + tempMoveDown * dt * 4.0f, 0, 1.0f);
_moveAccel = Mathf.Clamp(_moveAccel + tempMoveAccel * dt * 4.0f, 0, 1.0f);
}

private void UpdateCamera(float dt)
{
float scrollZoom = 0;

float x = (_moveRight - _moveLeft) * 2.0f;
float y = (_moveFront - _moveBack) + scrollZoom * 4.0f;
scrollZoom = 0;

var forward = transform.forward;
var right = transform.right;

// forward.y = 0f;
// right.y = 0;
Vector3 moveDirection = (forward * y + right * x).normalized;

float keySpeed = 22.0f;
if (0 < _moveAccel)
{
keySpeed *= _moveAccel * 2.0f;
}

transform.Translate(moveDirection * (keySpeed * dt), Space.World);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ GameObject:
- component: {fileID: 963194228}
- component: {fileID: 963194227}
- component: {fileID: 963194226}
- component: {fileID: 963194229}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
Expand Down Expand Up @@ -306,6 +307,18 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &963194229
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 534e5fd993f693741ad3c856b6745196, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &1131081564
PrefabInstance:
m_ObjectHideFlags: 0
Expand All @@ -316,7 +329,7 @@ PrefabInstance:
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 561964b985803e84da1e40e19b44707d, type: 3}
propertyPath: m_RootOrder
value: 5
value: 3
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 561964b985803e84da1e40e19b44707d, type: 3}
propertyPath: m_LocalPosition.x
Expand Down Expand Up @@ -458,13 +471,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1366844031}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -0.29694974, y: -41.08409, z: 36.32854}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_Father: {fileID: 1922218971}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1366844034
MonoBehaviour:
Expand Down Expand Up @@ -506,13 +519,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1736068886}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -0.29694974, y: -41.08409, z: 36.32854}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_Father: {fileID: 1922218971}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1736068889
MonoBehaviour:
Expand All @@ -531,3 +544,36 @@ MonoBehaviour:
constrainByCircle: 1
includeFlags: 65535
excludeFlags: 0
--- !u!1 &1922218970
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1922218971}
m_Layer: 0
m_Name: Toolset
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1922218971
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1922218970}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.29694974, y: 41.08409, z: -36.32854}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1736068888}
- {fileID: 1366844033}
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a9cb69

Please sign in to comment.