Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| wiki:prototyping [2022/08/12 00:13] – admin | wiki:prototyping [2022/08/15 23:52] (current) – [Prototyping Zombie Minigame] admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Prototyping Zombie Minigame ====== | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | <file| Prototyping?> | ||
| + | This is the way I make function of models before I make it official. | ||
| + | </ | ||
| ====== Player Prototyping ====== | ====== Player Prototyping ====== | ||
| {{ : | {{ : | ||
| - | ====== | + | <code CSharp | ZombiePlayerController Script> |
| + | using System.Collections; | ||
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| + | using DG.Tweening; | ||
| + | using Micosmo.SensorToolkit; | ||
| + | using System; | ||
| + | using Sirenix.OdinInspector; | ||
| + | using UnityEngine.Events; | ||
| + | using HighlightPlus; | ||
| + | |||
| + | public class ZombiePlayerController : MonoBehaviour | ||
| + | { | ||
| + | [BoxGroup(" | ||
| + | public GameObject cross; | ||
| + | [BoxGroup(" | ||
| + | public Transform charater; | ||
| + | |||
| + | [BoxGroup(" | ||
| + | public float lookAtSpeed | ||
| + | |||
| + | [BoxGroup(" | ||
| + | public float movementSpeed | ||
| + | [BoxGroup(" | ||
| + | public float movementSprintSpeed | ||
| + | [BoxGroup(" | ||
| + | public float jumpHeight | ||
| + | [BoxGroup(" | ||
| + | public float groundGravity | ||
| + | [BoxGroup(" | ||
| + | public float runStamina | ||
| + | |||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private bool _groundedStatus; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private Vector3 _finalMoveValue; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private float _fowardDirection; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private float _righDirection; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private float _velocity; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private float _storedRunStamina; | ||
| + | |||
| + | private CharacterController _characterController; | ||
| + | private ZombieHealth _zombieHealth; | ||
| + | private HighlightEffect _highlight; | ||
| + | |||
| + | [HideInEditorMode] | ||
| + | public UnityEvent< | ||
| + | |||
| + | [HideInEditorMode] | ||
| + | public UnityEvent onPlayerDied; | ||
| + | |||
| + | private void Awake() | ||
| + | { | ||
| + | Initialization(); | ||
| + | } | ||
| + | |||
| + | private void Start() | ||
| + | { | ||
| + | _storedRunStamina | ||
| + | _zombieHealth.sendEventOnMinusHealth.AddListener(ReceivedEventOnPlayerHit); | ||
| + | SendEventToUI(); | ||
| + | } | ||
| + | |||
| + | private void Initialization() | ||
| + | { | ||
| + | _characterController | ||
| + | _zombieHealth | ||
| + | _highlight | ||
| + | } | ||
| + | |||
| + | private void Update() | ||
| + | { | ||
| + | |||
| + | if (_zombieHealth.IsDead()) | ||
| + | { | ||
| + | onPlayerDied.Invoke(); | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | _velocity | ||
| + | |||
| + | SetRightFowardDirection(); | ||
| + | |||
| + | PlayerLookAtCross(); | ||
| + | |||
| + | //this will update _movement value that will be called by MovePlayer(_movement); | ||
| + | SetFinalMoveValue(); | ||
| + | |||
| + | MovePlayer(_finalMoveValue); | ||
| + | } | ||
| + | |||
| + | #region INPUT | ||
| + | private Vector3 InputRelativeToCamera() | ||
| + | { | ||
| + | //reading the input: | ||
| + | float horizontalAxis | ||
| + | float verticalAxis = Input.GetAxis(" | ||
| + | |||
| + | //assuming we only using the single camera: | ||
| + | var camera = Camera.main; | ||
| + | |||
| + | //camera forward and right vectors: | ||
| + | var forward = camera.transform.forward; | ||
| + | var right = camera.transform.right; | ||
| + | |||
| + | //project forward and right vectors on the horizontal plane (y = 0) | ||
| + | forward.y = 0f; | ||
| + | right.y = 0f; | ||
| + | forward.Normalize(); | ||
| + | right.Normalize(); | ||
| + | |||
| + | //this is the direction in the world space we want to move: | ||
| + | var desiredMoveDirection = (forward * verticalAxis + right * horizontalAxis).normalized * movementSpeed; | ||
| + | |||
| + | SprintController(); | ||
| + | |||
| + | if (Input.GetKey(KeyCode.LeftShift) && runStamina >= 0) | ||
| + | { | ||
| + | return desiredMoveDirection * movementSprintSpeed; | ||
| + | } | ||
| + | |||
| + | return desiredMoveDirection; | ||
| + | } | ||
| + | |||
| + | private void SprintController() | ||
| + | { | ||
| + | SendEventToUI(); | ||
| + | |||
| + | if(GetVelocity() > 5) | ||
| + | { | ||
| + | runStamina -= Time.deltaTime; | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | if (runStamina < _storedRunStamina && GetVelocity() == 0) runStamina += Time.deltaTime; | ||
| + | } | ||
| + | |||
| + | private float _jumpDuration = 0; | ||
| + | |||
| + | private float InputMovementJump() | ||
| + | { | ||
| + | |||
| + | if (!_groundedStatus) | ||
| + | { | ||
| + | _jumpDuration -= Time.deltaTime * groundGravity; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | _jumpDuration = Input.GetAxis(" | ||
| + | } | ||
| + | |||
| + | return _jumpDuration; | ||
| + | } | ||
| + | # | ||
| + | |||
| + | #region MONOBEHAVIOUR | ||
| + | private void SetRightFowardDirection() | ||
| + | { | ||
| + | //This method will take dot value to detect character moving direction in local axis (so kalau axis tu rotate dia akan rotate sama) | ||
| + | _righDirection = Vector3.Dot(transform.right, | ||
| + | _fowardDirection = Vector3.Dot(transform.forward, | ||
| + | } | ||
| + | |||
| + | private void PlayerLookAtCross() => charater.DOLookAt(cross.transform.position, | ||
| + | |||
| + | private void SetFinalMoveValue() | ||
| + | { | ||
| + | Vector3 _moveDirectionAtXZ = InputRelativeToCamera(); | ||
| + | float _jumpDuration = InputMovementJump(); | ||
| + | |||
| + | _finalMoveValue = new Vector3(_moveDirectionAtXZ.x, | ||
| + | |||
| + | } | ||
| + | |||
| + | private void MovePlayer(Vector3 _finalMoveValue) => _characterController.Move(_finalMoveValue * Time.deltaTime); | ||
| + | # | ||
| + | |||
| + | #region SET METHOD | ||
| + | public void SetWhenGrounded() => _groundedStatus = true; | ||
| + | |||
| + | public void SetWhenNotGrounded() => _groundedStatus = false; | ||
| + | # | ||
| + | |||
| + | #region GET METHOD | ||
| + | public bool IsGroundedStatus() => _groundedStatus; | ||
| + | |||
| + | public bool GetCursor() => Cursor.visible; | ||
| + | |||
| + | public float GetFowardDiretion() => _fowardDirection; | ||
| + | |||
| + | public float GetRightDirection() => _righDirection; | ||
| + | |||
| + | public float GetVelocity() => _characterController.velocity.magnitude; | ||
| + | # | ||
| + | |||
| + | #region EVENT | ||
| + | private void InitializeEvent() | ||
| + | { | ||
| + | if (sendEventToUI == null) sendEventToUI = new UnityEvent< | ||
| + | SendEventToUI(); | ||
| + | } | ||
| + | |||
| + | private void SendEventToUI() | ||
| + | { | ||
| + | sendEventToUI.Invoke(runStamina); | ||
| + | } | ||
| + | |||
| + | private void ReceivedEventOnPlayerHit() | ||
| + | { | ||
| + | _highlight.HitFX(); | ||
| + | } | ||
| + | # endregion | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code CSharp | ZombieGunController Script> | ||
| + | using System.Collections; | ||
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| + | using Sirenix.OdinInspector; | ||
| + | using HellTap.PoolKit; | ||
| + | using UnityEngine.Events; | ||
| + | using SkyConsole.Utils; | ||
| + | |||
| + | public class ZombieGunController : MonoBehaviour | ||
| + | { | ||
| + | [BoxGroup(" | ||
| + | public Spawner gunBarrelPoolSpawner; | ||
| + | [BoxGroup(" | ||
| + | public float bulletSpeed; | ||
| + | [BoxGroup(" | ||
| + | public float shootCooldown = 1f; | ||
| + | [BoxGroup(" | ||
| + | private int bullet = 10; | ||
| + | [BoxGroup(" | ||
| + | public AudioClip shootSound; | ||
| + | [BoxGroup(" | ||
| + | public AudioClip emptyShootSound; | ||
| + | [BoxGroup(" | ||
| + | public AudioSource gunSoundSource; | ||
| + | |||
| + | [HideInEditorMode] | ||
| + | public UnityEvent< | ||
| + | |||
| + | private void Start() | ||
| + | { | ||
| + | gunSoundSource = this.GetComponent< | ||
| + | InitializeEvent(); | ||
| + | } | ||
| + | |||
| + | private void Update() | ||
| + | { | ||
| + | BulletShootHandler(); | ||
| + | } | ||
| + | |||
| + | [SerializeField] | ||
| + | private float timer = 0; | ||
| + | private void BulletShootHandler() | ||
| + | { | ||
| + | if (timer > 0) | ||
| + | { | ||
| + | timer -= Time.deltaTime; | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | if (Input.GetKey(KeyCode.Mouse0)) | ||
| + | { | ||
| + | timer = shootCooldown; | ||
| + | |||
| + | if (bullet == 0) | ||
| + | { | ||
| + | PlaySound(emptyShootSound); | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | if (gunBarrelPoolSpawner != null) | ||
| + | { | ||
| + | gunBarrelPoolSpawner.Play(); | ||
| + | PlaySound(shootSound); | ||
| + | bullet -= 1; | ||
| + | SendEventToUI(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | private void PlaySound(AudioClip sound) | ||
| + | { | ||
| + | gunSoundSource.PlayOneShot(sound); | ||
| + | } | ||
| + | |||
| + | |||
| + | #region EVENT | ||
| + | private void InitializeEvent() | ||
| + | { | ||
| + | if (sendEventToUI == null) sendEventToUI = new UnityEvent< | ||
| + | SendEventToUI(); | ||
| + | } | ||
| + | |||
| + | private void SendEventToUI() | ||
| + | { | ||
| + | sendEventToUI.Invoke(bullet); | ||
| + | } | ||
| + | # | ||
| + | |||
| + | public void addBullet(int bulletCount) | ||
| + | { | ||
| + | bullet += bulletCount; | ||
| + | SendEventToUI(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | <code CSharp | ZombieCameraController Script> | ||
| + | using System.Collections; | ||
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| + | using Micosmo.SensorToolkit; | ||
| + | using DG.Tweening; | ||
| + | using System; | ||
| + | using Sirenix.OdinInspector; | ||
| + | |||
| + | public class ZombieCameraController : MonoBehaviour | ||
| + | { | ||
| + | [BoxGroup(" | ||
| + | public GameObject cross; | ||
| + | [BoxGroup(" | ||
| + | public GameObject crossCylinder; | ||
| + | [BoxGroup(" | ||
| + | public GameObject player; | ||
| + | |||
| + | [BoxGroup(" | ||
| + | public float crossSpeed = 1f; | ||
| + | |||
| + | [BoxGroup(" | ||
| + | public float zTilt; | ||
| + | [BoxGroup(" | ||
| + | public float xTilt; | ||
| + | |||
| + | private Camera _camera; | ||
| + | private RaySensor _sensor; | ||
| + | private RangeSensor _crossRangeSensor; | ||
| + | |||
| + | private void Awake() | ||
| + | { | ||
| + | _sensor = this.GetComponent< | ||
| + | _camera = this.GetComponent< | ||
| + | _crossRangeSensor = cross.GetComponentInChildren< | ||
| + | } | ||
| + | |||
| + | private void Update() | ||
| + | { | ||
| + | MoveRaycastToMousePosition(); | ||
| + | CameraMovement(); | ||
| + | SetCrossCylinderActive(); | ||
| + | } | ||
| + | |||
| + | private void MoveRaycastToMousePosition() | ||
| + | { | ||
| + | var ray = _camera.ScreenPointToRay(Input.mousePosition); | ||
| + | _sensor.Direction = ray.direction; | ||
| + | |||
| + | MoveCrossToRaycastObstructionRayHit(); | ||
| + | } | ||
| + | |||
| + | private void MoveCrossToRaycastObstructionRayHit() | ||
| + | { | ||
| + | float x = _sensor.GetObstructionRayHit().Point.x; | ||
| + | float z = _sensor.GetObstructionRayHit().Point.z; | ||
| + | |||
| + | if (_sensor.IsObstructed) cross.transform.DOMove(new Vector3(x, cross.transform.position.y, | ||
| + | } | ||
| + | |||
| + | private void CameraMovement() | ||
| + | { | ||
| + | this.transform.DOLookAt(player.transform.position, | ||
| + | |||
| + | float x = player.transform.position.x + xTilt; | ||
| + | float z = player.transform.position.z + zTilt; | ||
| + | this.transform.DOMove(new Vector3(x, this.transform.position.y, | ||
| + | } | ||
| + | |||
| + | private void SetCrossCylinderActive() | ||
| + | { | ||
| + | if (_crossRangeSensor.GetNearestDetection() != null) | ||
| + | { | ||
| + | crossCylinder.SetActive(true); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | crossCylinder.SetActive(false); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <code CSharp | ZombieBullet Script> | ||
| + | using Micosmo.SensorToolkit; | ||
| + | using System.Collections; | ||
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| + | using DG.Tweening; | ||
| + | using HellTap.PoolKit; | ||
| + | using Sirenix.OdinInspector; | ||
| + | using SkyConsole.Utils; | ||
| + | |||
| + | public class ZombieBullet : MonoBehaviour | ||
| + | { | ||
| + | public float bulletSpeed = 25f; | ||
| + | public ParticleSystem bulletCollideParticle; | ||
| + | public float bulletDamage = 10; | ||
| + | public RangeSensor _rangeSensor; | ||
| + | private MeshRenderer _meshRenderer; | ||
| + | private Pool _pool; | ||
| + | |||
| + | private bool canMove = true; | ||
| + | |||
| + | private void Awake() | ||
| + | { | ||
| + | _meshRenderer = this.GetComponent< | ||
| + | _pool = FindObjectOfType< | ||
| + | |||
| + | _rangeSensor.OnSomeDetection.AddListener(ReceivedListenerOnCollided); | ||
| + | } | ||
| + | |||
| + | private void Update() | ||
| + | { | ||
| + | moveBullet(canMove); | ||
| + | } | ||
| + | |||
| + | private void moveBullet(bool canMove) | ||
| + | { | ||
| + | if(canMove == true) transform.position += transform.forward * Time.deltaTime * bulletSpeed; | ||
| + | } | ||
| + | |||
| + | private IEnumerator DespawnGameObject(float duration) | ||
| + | { | ||
| + | |||
| + | yield return new WaitForSeconds(duration); | ||
| + | |||
| + | canMove = true; | ||
| + | _pool.Despawn(this.gameObject); | ||
| + | } | ||
| + | |||
| + | #region Called From Event & Outside | ||
| + | private void ReceivedListenerOnCollided() | ||
| + | { | ||
| + | canMove = false; | ||
| + | bulletCollideParticle.Play(); | ||
| + | |||
| + | if(_rangeSensor.GetNearestDetection().TryGetComponent(out ZombieHealth zombieHealth)) | ||
| + | { | ||
| + | zombieHealth.MinusHealth(bulletDamage); | ||
| + | } | ||
| + | |||
| + | StartCoroutine(DespawnGameObject(bulletCollideParticle.main.duration)); | ||
| + | } | ||
| + | |||
| + | private void OnDisable() | ||
| + | { | ||
| + | _rangeSensor.enabled = false; | ||
| + | } | ||
| + | |||
| + | private void OnEnable() | ||
| + | { | ||
| + | _rangeSensor.enabled = true; | ||
| + | } | ||
| + | # | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| - | {{ : | ||