Welcome to GEMENTAR TOUR PUO V2 Development Documentation. Enjoy your stay!

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
wiki:prototyping [2022/08/12 00:27] – [NPC Prototyping] adminwiki:prototyping [2022/08/15 23:52] (current) – [Prototyping Zombie Minigame] admin
Line 1: Line 1:
-====== Player Prototyping ======+====== Prototyping Zombie Minigame ======
  
-{{ :wiki:zombieprototype.gif?nolink |}}+{{ :wiki:image_2022-08-16_075049458.png?600 |}}
  
-====== NPC Prototyping ======+<file| Prototyping?> 
 +This is the way I make function of models before I make it official. 
 +</file> 
 +====== Player Prototyping ======
  
-{{ :wiki:zombieprototypenpc.gif |}}+{{ :wiki:zombieprototype.gif?nolink |}}
  
-<code CSharp | ZombieNpcMovingController Script>+<code CSharp | ZombiePlayerController Script>
 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 : MonoBehaviour+public class ZombiePlayerController : MonoBehaviour
 { {
-    public List<Transform> checkPoint; +    [BoxGroup("GameObject Serialization")] 
-    private int checkPointIndex = 0;+    public GameObject cross; 
 +    [BoxGroup("GameObject Serialization")] 
 +    public Transform charater;
  
-    private CharacterController _characterController; +    [BoxGroup("DoLookAt Fine Tune")] 
-    private ZombieAnimationController _zombieAnimationController;+    public float lookAtSpeed = 1f;
  
-    [BoxGroup("Movement Tweak")] [SerializeField] private float _walkSpeed = 1; +    [BoxGroup("Movement Fine Tune")] 
-    [BoxGroup("Movement Tweak")] [SerializeField] private float _runSpeed = 3;+    public float movementSpeed = 1f; 
 +    [BoxGroup("Movement Fine Tune")] 
 +    public float movementSprintSpeed = 1f; 
 +    [BoxGroup("Movement Fine Tune")] 
 +    public float jumpHeight = 1f; 
 +    [BoxGroup("Movement Fine Tune")] 
 +    public float groundGravity = 1f; 
 +    [BoxGroup("Movement Fine Tune")] 
 +    public float runStamina = 15f;
  
-    private ISteeringSensor _steering; +    [BoxGroup("Private Variable")] 
-    private SteeringSensor _steeringSensor;+    [ShowInInspector] 
 +    private bool _groundedStatus; 
 +    [BoxGroup("Private Variable")] 
 +    [ShowInInspector] 
 +    private Vector3 _finalMoveValue; 
 +    [BoxGroup("Private Variable")] 
 +    [ShowInInspector] 
 +    private float _fowardDirection; 
 +    [BoxGroup("Private Variable")] 
 +    [ShowInInspector] 
 +    private float _righDirection; 
 +    [BoxGroup("Private Variable")] 
 +    [ShowInInspector] 
 +    private float _velocity; 
 +    [BoxGroup("Private Variable")] 
 +    [ShowInInspector] 
 +    private float _storedRunStamina;
  
 +    private CharacterController _characterController;
 +    private ZombieHealth _zombieHealth;
 +    private HighlightEffect _highlight;
  
-    private void Start()+    [HideInEditorMode] 
 +    public UnityEvent<float> sendEventToUI; 
 + 
 +    [HideInEditorMode] 
 +    public UnityEvent onPlayerDied; 
 + 
 +    private void Awake()
     {     {
         Initialization();         Initialization();
 +    }
  
-        _steeringSensor.Locomotion.MaxForwardSpeed = _walkSpeed;+    private void Start() 
 +    { 
 +        _storedRunStamina = runStamina; 
 +        _zombieHealth.sendEventOnMinusHealth.AddListener(ReceivedEventOnPlayerHit); 
 +        SendEventToUI();
     }     }
  
     private void Initialization()     private void Initialization()
     {     {
-        _steering = GetComponentInChildren<ISteeringSensor>(); +        _characterController = this.GetComponent<CharacterController>(); 
-        _steeringSensor = GetComponentInChildren<SteeringSensor>(); +        _zombieHealth = this.GetComponent<ZombieHealth>(); 
-        _characterController = GetComponent<CharacterController>(); +        _highlight = this.GetComponent<HighlightEffect>();
-        _zombieAnimationController = GetComponent<ZombieAnimationController>();+
     }     }
  
-    void Update()+    private 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 void MoveToCheckPoint()+    #region INPUT 
 +    private Vector3 InputRelativeToCamera() 
     {     {
-        if (_steering.IsDestinationReached)+        //reading the input: 
 +        float horizontalAxis = Input.GetAxis("Horizontal"); 
 +        float verticalAxis = Input.GetAxis("Vertical"); 
 + 
 +        //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)
         {         {
-            int index = checkPointIndex == checkPoint.Count - 1 ? checkPointIndex = 0 : checkPointIndex += 1;+            return desiredMoveDirection * movementSprintSpeed; 
 +        }
  
-            _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("Jump");
 +        }
 +
 +        return _jumpDuration;
     }     }
 +    #endregion
  
-    [Button] +    #region MONOBEHAVIOUR 
-    public void MakeCharacterWalk()+    private void SetRightFowardDirection()
     {     {
-        _steeringSensor.Locomotion.MaxForwardSpeed = _walkSpeed;+        //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, new Vector3(_finalMoveValue.x, 0f, _finalMoveValue.z)); 
 +        _fowardDirection = Vector3.Dot(transform.forward, new Vector3(_finalMoveValue.x, 0, _finalMoveValue.z));
     }     }
  
-    [Button] +    private void PlayerLookAtCross() => charater.DOLookAt(cross.transform.position, lookAtSpeed, AxisConstraint.Y); 
-    public void MakeCharacterRunning()+ 
 +    private void SetFinalMoveValue()
     {     {
-        _steeringSensor.Locomotion.MaxForwardSpeed = _runSpeed;+        Vector3 _moveDirectionAtXZ = InputRelativeToCamera(); 
 +        float _jumpDuration = InputMovementJump(); 
 + 
 +        _finalMoveValue = new Vector3(_moveDirectionAtXZ.x, _jumpDuration * jumpHeight, _moveDirectionAtXZ.z); 
     }     }
  
-    [Button] +    private void MovePlayer(Vector3 _finalMoveValue) =>  _characterController.Move(_finalMoveValue * Time.deltaTime); 
-    public void MakeCharacterPunch()+    #endregion 
 + 
 +    #region SET METHOD 
 +    public void SetWhenGrounded() => _groundedStatus = true; 
 + 
 +    public void SetWhenNotGrounded() =>  _groundedStatus = false; 
 +    #endregion 
 + 
 +    #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; 
 +    #endregion 
 + 
 +    #region EVENT 
 +    private void InitializeEvent()
     {     {
-        _zombieAnimationController.PlayAction(_zombieAnimationController.attack);+        if (sendEventToUI == null) sendEventToUI = new UnityEvent<float>(); 
 +        SendEventToUI();
     }     }
 +
 +    private void SendEventToUI()
 +    {
 +        sendEventToUI.Invoke(runStamina);
 +    }
 +
 +    private void ReceivedEventOnPlayerHit()
 +    {
 +        _highlight.HitFX();
 +    }
 +    # endregion
 } }
 +
 </code> </code>
  
-<code CSharp | ZombieAnimationController Script> +<code CSharp | ZombieGunController Script> 
-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("GameObject Initialization")]
 +    public Spawner gunBarrelPoolSpawner;
 +    [BoxGroup("Weapon Porperties")]
 +    public float bulletSpeed;
 +    [BoxGroup("Weapon Porperties")]
 +    public float shootCooldown = 1f;
 +    [BoxGroup("Weapon Porperties")]
 +    private int bullet = 10;
 +    [BoxGroup("Weapon Porperties")]
 +    public AudioClip shootSound;
 +    [BoxGroup("Weapon Porperties")]
 +    public AudioClip emptyShootSound;
 +    [BoxGroup("Weapon Porperties")]
 +    public AudioSource gunSoundSource;
 +
 +    [HideInEditorMode]
 +    public UnityEvent<int> sendEventToUI;
 +
 +    private void Start()
 +    {
 +        gunSoundSource = this.GetComponent<AudioSource>();
 +        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<int>();
 +        SendEventToUI();
 +    }
 +
 +    private void SendEventToUI()
 +    {
 +        sendEventToUI.Invoke(bullet);
 +    }
 +    #endregion
 +
 +    public void addBullet(int bulletCount)
 +    {
 +        bullet += bulletCount;
 +        SendEventToUI();
 +    }
 +}
 +
 +
 +
 +</code>
 +
 +<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 : MonoBehaviour+public class ZombieCameraController : MonoBehaviour
 { {
-    private AnimancerComponent _Animancer; +    [BoxGroup("Serialize GameObject")] 
-    [SerializeField] public ClipTransition idle; +    public GameObject cross; 
-    [SerializeField] public ClipTransition moveFoward; +    [BoxGroup("Serialize GameObject")] 
-    [SerializeField] public ClipTransition runFoward; +    public GameObject crossCylinder; 
-    [SerializeField] public ClipTransition attack; +    [BoxGroup("Serialize GameObject")] 
-    [SerializeField] private AvatarMask _baseBody; +    public GameObject player;
-    [SerializeField] private AvatarMask _upperBody;+
  
-    private AnimancerLayer _baseLayer; +    [BoxGroup("Cross Fine Tuning")] 
-    private AnimancerLayer _upperBodyLayer;+    public float crossSpeed = 1f;
  
-    //private CharacterController _characterController; +    [BoxGroup("Camera Porperties")] 
-    private NavMeshAgent _navMeshAgent;+    public float zTilt; 
 +    [BoxGroup("Camera Porperties")] 
 +    public float xTilt; 
 + 
 +    private Camera _camera; 
 +    private RaySensor _sensor; 
 +    private RangeSensor _crossRangeSensor;
  
     private void Awake()     private void Awake()
     {     {
-        Initialization();+        _sensor = this.GetComponent<RaySensor>(); 
 +        _camera = this.GetComponent<Camera>(); 
 +        _crossRangeSensor = cross.GetComponentInChildren<RangeSensor>(); 
 +    }
  
-        InitializationAnim();+    private void Update() 
 +    { 
 +        MoveRaycastToMousePosition(); 
 +        CameraMovement(); 
 +        SetCrossCylinderActive();
     }     }
  
-    private void Initialization()+    private void MoveRaycastToMousePosition()
     {     {
-        //_characterController = GetComponent<CharacterController>(); +        var ray = _camera.ScreenPointToRay(Input.mousePosition); 
-        _navMeshAgent = GetComponent<NavMeshAgent>(); +        _sensor.Direction = ray.direction; 
-        _Animancer = GetComponentInChildren<AnimancerComponent>();+ 
 +        MoveCrossToRaycastObstructionRayHit();
     }     }
  
-    private void InitializationAnim()+    private void MoveCrossToRaycastObstructionRayHit()
     {     {
-        _baseLayer = _Animancer.Layers[0]; +        float x = _sensor.GetObstructionRayHit().Point.x; 
-        _upperBodyLayer = _Animancer.Layers[1];+        float z = _sensor.GetObstructionRayHit().Point.z;
  
-        _baseLayer.SetMask(_baseBody); +        if (_sensor.IsObstructed) cross.transform.DOMove(new Vector3(x, cross.transform.position.y, z), crossSpeed);
-        _upperBodyLayer.SetMask(_upperBody); +
-        attack.Events.OnEnd = OnActionEnd;+
     }     }
  
-    private void Update()+    private void CameraMovement()
     {     {
-        //if(_characterController.velocity.magnitude > 1f) +        this.transform.DOLookAt(player.transform.position, 1f); 
-        if (_navMeshAgent.velocity.magnitude > 1f)+ 
 +        float x = player.transform.position.x + xTilt; 
 +        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)
         {         {
-            _baseLayer.Play(moveFoward, 1f);+            crossCylinder.SetActive(true);
         }         }
- +        else
-        //if (_characterController.velocity.magnitude > 2f) +
-        if(_navMeshAgent.velocity.magnitude > 2f)+
         {         {
-            _baseLayer.Play(runFoward, 1f);+            crossCylinder.SetActive(false);
         }         }
     }     }
 +}
  
-    bool _canBePlayed = true; +</code> 
-    public void PlayAction(ClipTransition clip)+ 
 +<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()
     {     {
-        if (!_canBePlayed) return;+        _meshRenderer = this.GetComponent<MeshRenderer>(); 
 +        _pool = FindObjectOfType<Pool>();
  
-        _upperBodyLayer.Play(clip);+        _rangeSensor.OnSomeDetection.AddListener(ReceivedListenerOnCollided); 
 +    }
  
-        StartCoroutine(PlayActionDelay(clip.Clip.length));+    private void Update() 
 +    { 
 +        moveBullet(canMove);
     }     }
  
-    private IEnumerator PlayActionDelay(float length)+    private void moveBullet(bool canMove)
     {     {
-        _canBePlayed = false; +        if(canMove == true) transform.position += transform.forward * Time.deltaTime * bulletSpeed;
-        yield return new WaitForSeconds(length); +
-        _canBePlayed = true;+
     }     }
  
-    private void OnActionEnd()+    private IEnumerator DespawnGameObject(float duration)
     {     {
-        _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
 +
 } }
 +
  
 </code> </code>
  

QR Code
QR Code wiki:prototyping (generated for current page)
Hello World!
DokuWiki with monobook... rules!