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:27] – [NPC Prototyping] admin | wiki:prototyping [2022/08/15 23:52] (current) – [Prototyping Zombie Minigame] admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== Prototyping |
| - | {{ :wiki:zombieprototype.gif?nolink | + | {{ :wiki:image_2022-08-16_075049458.png?600 |}} |
| - | ====== | + | <file| Prototyping?> |
| + | This is the way I make function of models before I make it official. | ||
| + | </ | ||
| + | ====== | ||
| - | {{ :wiki:zombieprototypenpc.gif |}} | + | {{ :wiki:zombieprototype.gif? |
| - | <code CSharp | ZombieNpcMovingController | + | <code CSharp | ZombiePlayerController |
| using System.Collections; | using System.Collections; | ||
| using System.Collections.Generic; | using System.Collections.Generic; | ||
| Line 13: | Line 16: | ||
| using DG.Tweening; | using DG.Tweening; | ||
| using Micosmo.SensorToolkit; | using Micosmo.SensorToolkit; | ||
| + | using System; | ||
| using Sirenix.OdinInspector; | using Sirenix.OdinInspector; | ||
| + | using UnityEngine.Events; | ||
| + | using HighlightPlus; | ||
| - | public class ZombieNpcMovingController | + | public class ZombiePlayerController |
| { | { | ||
| - | public | + | |
| - | | + | |
| + | | ||
| + | public Transform charater; | ||
| - | | + | |
| - | | + | |
| - | [BoxGroup(" | + | [BoxGroup(" |
| - | [BoxGroup(" | + | public float movementSpeed = 1f; |
| + | | ||
| + | public | ||
| + | [BoxGroup(" | ||
| + | public float jumpHeight = 1f; | ||
| + | | ||
| + | public float groundGravity = 1f; | ||
| + | [BoxGroup(" | ||
| + | public | ||
| - | private | + | |
| - | private | + | [ShowInInspector] |
| + | | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private Vector3 _finalMoveValue; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private float _fowardDirection; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private float _righDirection; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private float _velocity; | ||
| + | [BoxGroup(" | ||
| + | [ShowInInspector] | ||
| + | private | ||
| + | private CharacterController _characterController; | ||
| + | private ZombieHealth _zombieHealth; | ||
| + | private HighlightEffect _highlight; | ||
| - | private void Start() | + | |
| + | public UnityEvent< | ||
| + | |||
| + | [HideInEditorMode] | ||
| + | public UnityEvent onPlayerDied; | ||
| + | |||
| + | | ||
| { | { | ||
| Initialization(); | Initialization(); | ||
| + | } | ||
| - | _steeringSensor.Locomotion.MaxForwardSpeed = _walkSpeed; | + | private void Start() |
| + | { | ||
| + | _storedRunStamina = runStamina; | ||
| + | _zombieHealth.sendEventOnMinusHealth.AddListener(ReceivedEventOnPlayerHit); | ||
| + | SendEventToUI(); | ||
| } | } | ||
| private void Initialization() | private void Initialization() | ||
| { | { | ||
| - | | + | |
| - | | + | |
| - | _characterController | + | |
| - | | + | |
| } | } | ||
| - | void Update() | + | |
| { | { | ||
| - | MoveToCheckPoint(); | + | |
| + | if (_zombieHealth.IsDead()) | ||
| + | { | ||
| + | onPlayerDied.Invoke(); | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | _velocity = GetVelocity(); | ||
| + | |||
| + | SetRightFowardDirection(); | ||
| + | |||
| + | PlayerLookAtCross(); | ||
| + | |||
| + | //this will update _movement value that will be called by MovePlayer(_movement); | ||
| + | SetFinalMoveValue(); | ||
| + | |||
| + | MovePlayer(_finalMoveValue); | ||
| } | } | ||
| - | private | + | |
| + | | ||
| { | { | ||
| - | if (_steering.IsDestinationReached) | + | |
| + | float horizontalAxis = Input.GetAxis(" | ||
| + | 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(); | ||
| + | |||
| + | | ||
| { | { | ||
| - | | + | |
| + | } | ||
| - | _steering.Seek.DestinationTransform = checkPoint[index]; | + | 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; | ||
| } | } | ||
| + | #endregion | ||
| - | | + | |
| - | | + | |
| { | { | ||
| - | | + | |
| + | _righDirection = Vector3.Dot(transform.right, new Vector3(_finalMoveValue.x, | ||
| + | _fowardDirection | ||
| } | } | ||
| - | | + | |
| - | | + | |
| + | | ||
| { | { | ||
| - | | + | |
| + | float _jumpDuration = InputMovementJump(); | ||
| + | |||
| + | _finalMoveValue = new Vector3(_moveDirectionAtXZ.x, _jumpDuration * jumpHeight, _moveDirectionAtXZ.z); | ||
| } | } | ||
| - | | + | |
| - | public void MakeCharacterPunch() | + | # |
| + | |||
| + | #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() | ||
| { | { | ||
| - | | + | |
| + | SendEventToUI(); | ||
| } | } | ||
| + | |||
| + | private void SendEventToUI() | ||
| + | { | ||
| + | sendEventToUI.Invoke(runStamina); | ||
| + | } | ||
| + | |||
| + | private void ReceivedEventOnPlayerHit() | ||
| + | { | ||
| + | _highlight.HitFX(); | ||
| + | } | ||
| + | # endregion | ||
| } | } | ||
| + | |||
| </ | </ | ||
| - | <code CSharp | ZombieAnimationController | + | <code CSharp | ZombieGunController |
| - | using Animancer; | + | using System.Collections; |
| + | using System.Collections.Generic; | ||
| + | using UnityEngine; | ||
| using Sirenix.OdinInspector; | 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); | ||
| + | } | ||
| + | #endregion | ||
| + | |||
| + | public void addBullet(int bulletCount) | ||
| + | { | ||
| + | bullet += bulletCount; | ||
| + | SendEventToUI(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | <code CSharp | ZombieCameraController Script> | ||
| using System.Collections; | using System.Collections; | ||
| using System.Collections.Generic; | using System.Collections.Generic; | ||
| using UnityEngine; | using UnityEngine; | ||
| - | using UnityEngine.AI; | + | using Micosmo.SensorToolkit; |
| + | using DG.Tweening; | ||
| + | using System; | ||
| + | using Sirenix.OdinInspector; | ||
| - | public class ZombieAnimationController | + | public class ZombieCameraController |
| { | { | ||
| - | | + | [BoxGroup(" |
| - | | + | public |
| - | | + | [BoxGroup(" |
| - | [SerializeField] public ClipTransition runFoward; | + | public |
| - | | + | [BoxGroup(" |
| - | [SerializeField] private AvatarMask _baseBody; | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | private | + | public float zTilt; |
| + | [BoxGroup(" | ||
| + | public float xTilt; | ||
| + | |||
| + | private Camera _camera; | ||
| + | | ||
| + | private | ||
| private void Awake() | private void Awake() | ||
| { | { | ||
| - | | + | |
| + | _camera = this.GetComponent< | ||
| + | _crossRangeSensor = cross.GetComponentInChildren< | ||
| + | } | ||
| - | InitializationAnim(); | + | private void Update() |
| + | { | ||
| + | MoveRaycastToMousePosition(); | ||
| + | CameraMovement(); | ||
| + | SetCrossCylinderActive(); | ||
| } | } | ||
| - | private void Initialization() | + | private void MoveRaycastToMousePosition() |
| { | { | ||
| - | | + | |
| - | | + | |
| - | | + | |
| + | | ||
| } | } | ||
| - | private void InitializationAnim() | + | private void MoveCrossToRaycastObstructionRayHit() |
| { | { | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | _upperBodyLayer.SetMask(_upperBody); | + | |
| - | attack.Events.OnEnd = OnActionEnd; | + | |
| } | } | ||
| - | private void Update() | + | private void CameraMovement() |
| { | { | ||
| - | | + | |
| - | | + | |
| + | | ||
| + | float z = player.transform.position.z + zTilt; | ||
| + | this.transform.DOMove(new Vector3(x, this.transform.position.y, z), 1f); | ||
| + | } | ||
| + | |||
| + | private void SetCrossCylinderActive() | ||
| + | { | ||
| + | if (_crossRangeSensor.GetNearestDetection() != null) | ||
| { | { | ||
| - | | + | |
| } | } | ||
| - | + | else | |
| - | //if (_characterController.velocity.magnitude > 2f) | + | |
| - | if(_navMeshAgent.velocity.magnitude > 2f) | + | |
| { | { | ||
| - | | + | |
| } | } | ||
| } | } | ||
| + | } | ||
| - | | + | </ |
| - | | + | |
| + | <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 | ||
| + | |||
| + | | ||
| { | { | ||
| - | | + | |
| + | _pool = FindObjectOfType< | ||
| - | | + | |
| + | } | ||
| - | StartCoroutine(PlayActionDelay(clip.Clip.length)); | + | private void Update() |
| + | { | ||
| + | moveBullet(canMove); | ||
| } | } | ||
| - | private | + | private |
| { | { | ||
| - | | + | |
| - | yield return new WaitForSeconds(length); | + | |
| - | _canBePlayed | + | |
| } | } | ||
| - | private | + | private |
| { | { | ||
| - | _upperBodyLayer.StartFade(0, 1f); | + | |
| + | 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; | ||
| + | } | ||
| + | #endregion | ||
| + | |||
| } | } | ||
| + | |||
| </ | </ | ||