|
| 1 | +/** |
| 2 | + * $File: JCS_3DCameraPlayer.cs $ |
| 3 | + * $Date: 2020-04-07 16:57:50 $ |
| 4 | + * $Revision: $ |
| 5 | + * $Creator: Jen-Chieh Shen $ |
| 6 | + * $Notice: See LICENSE.txt for modification and distribution information |
| 7 | + * Copyright © 2020 by Shen, Jen-Chieh $ |
| 8 | + */ |
| 9 | +using System.Collections; |
| 10 | +using System.Collections.Generic; |
| 11 | +using UnityEngine; |
| 12 | + |
| 13 | +namespace JCSUnity |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Player control that works with camera relatively. |
| 17 | + /// |
| 18 | + /// Reference game => `Monster Hunter`. |
| 19 | + /// </summary> |
| 20 | + public class JCS_3DCameraPlayer |
| 21 | + : JCS_3DPlayer |
| 22 | + { |
| 23 | + /* Variables */ |
| 24 | + |
| 25 | + private JCS_3DLookAtAction mLookAtAction = null; |
| 26 | + |
| 27 | + [Header("** Initialize Variables (JCS_3DCameraPlayer) **")] |
| 28 | + |
| 29 | + [Tooltip("Object the character want to look at.")] |
| 30 | + [SerializeField] |
| 31 | + private Transform mLookPoint = null; |
| 32 | + |
| 33 | + [Tooltip("Distance to place look point object.")] |
| 34 | + [SerializeField] |
| 35 | + [Range(0.0f, 50.0f)] |
| 36 | + private float mLookDistance = 5.0f; |
| 37 | + |
| 38 | + [Header("** Runtime Variables (JCS_3DCameraPlayer) **")] |
| 39 | + |
| 40 | + [Tooltip("How hard this player jumps.")] |
| 41 | + [SerializeField] |
| 42 | + [Range(0.0f, 300.0f)] |
| 43 | + private float mJumpForce = 10.0f; |
| 44 | + |
| 45 | + [Tooltip("Keys that goes forward.")] |
| 46 | + [SerializeField] |
| 47 | + private KeyCode mUpKey = KeyCode.UpArrow; |
| 48 | + |
| 49 | + [Tooltip("Keys that goes backward.")] |
| 50 | + [SerializeField] |
| 51 | + private KeyCode mDownKey = KeyCode.DownArrow; |
| 52 | + |
| 53 | + [Tooltip("Keys that goes right.")] |
| 54 | + [SerializeField] |
| 55 | + private KeyCode mRightKey = KeyCode.RightArrow; |
| 56 | + |
| 57 | + [Tooltip("Keys that goes left.")] |
| 58 | + [SerializeField] |
| 59 | + private KeyCode mLeftKey = KeyCode.LeftArrow; |
| 60 | + |
| 61 | + /* Setter & Getter */ |
| 62 | + |
| 63 | + /* Functions */ |
| 64 | + |
| 65 | + protected override void Awake() |
| 66 | + { |
| 67 | + base.Awake(); |
| 68 | + |
| 69 | + this.mLookAtAction = this.GetComponent<JCS_3DLookAtAction>(); |
| 70 | + mLookAtAction.SetTargetTransform(mLookPoint); |
| 71 | + } |
| 72 | + |
| 73 | + protected override void Update() |
| 74 | + { |
| 75 | + base.Update(); |
| 76 | + |
| 77 | + HandleFacing(); |
| 78 | + } |
| 79 | + |
| 80 | + protected override void FixedUpdate() |
| 81 | + { |
| 82 | + if (mCharacterController.enabled) |
| 83 | + { |
| 84 | + // apply force |
| 85 | + mCharacterController.Move(transform.forward * mVelocity.z * Time.deltaTime); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + protected override void PlayerInput() |
| 90 | + { |
| 91 | + if (JCS_Input.GetKey(mUpKey) || JCS_Input.GetKey(mDownKey) || |
| 92 | + JCS_Input.GetKey(mLeftKey) || JCS_Input.GetKey(mRightKey)) |
| 93 | + { |
| 94 | + mVelocity.z = mMoveSpeed; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + mVelocity.z = 0; |
| 99 | + } |
| 100 | + |
| 101 | + if (JCS_Input.GetKeyDown(KeyCode.Space)) |
| 102 | + mVelocity.y = mJumpForce; |
| 103 | + } |
| 104 | + |
| 105 | + private void HandleFacing() |
| 106 | + { |
| 107 | + if (mLookPoint == null) |
| 108 | + return; |
| 109 | + |
| 110 | + JCS_Camera cam = JCS_Camera.main; |
| 111 | + |
| 112 | + Vector3 newPos = this.transform.localPosition; |
| 113 | + Vector3 direction = Vector3.zero; |
| 114 | + |
| 115 | + if (JCS_Input.GetKey(mUpKey)) |
| 116 | + direction = cam.transform.forward; |
| 117 | + else if (JCS_Input.GetKey(mDownKey)) |
| 118 | + direction = -cam.transform.forward; |
| 119 | + |
| 120 | + if (JCS_Input.GetKey(mRightKey)) |
| 121 | + direction += cam.transform.right; |
| 122 | + else if (JCS_Input.GetKey(mLeftKey)) |
| 123 | + direction += -cam.transform.right; |
| 124 | + |
| 125 | + mLookPoint.localPosition = newPos + direction * mLookDistance; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments