Private void PerformDamage(IReadOnl圜ollection colliders) Public override void Enter(PlayerStateMachine parent) Weapons = new PlayerWeapons(_weaponsHand) Īnd in the PlayerAttack state we need to get the actual weapon damage if we are holding a weapon: namespace Public class PlayerStateMachine : StateMachine Now we just need to connect it to the Player’s state machine script: namespace Player Object.Instantiate(weapon.Prefab, _playerHand) Public void AttachWeapon(WeaponSO weapon) Public PlayerWeapons(Transform playerHand) Public float WeaponDamage => HasWeapon ? _lastWeapon.Damage : 0 Public bool HasWeapon => _lastWeapon != null & _lastWeapon.Prefab != null Using the same logic as the PlayerAnimations class we wrote on the previous tutorial, we now can create a PlayerWeapons class to handle the logic of the weapons in the player’s state machine: using UnityEngine Return Physics.OverlapBox(bounds.center, bounds.extents / 2f, Quaternion.identity, TargetMask) we need to check if we have a weapon and if so we need to add the boundsīounds.Encapsulate(Weapon.GetBoundsRelativeToPlayer(origin)) Var bounds = GetBoundsRelativeToPlayer(origin, isFacingRight) Public Collider Hit(Transform origin, bool isFacingRight) / Performs a Physics query to check for colliders in a specific point NewItem.CreateInstance(weapon, _spawnForce) Var newItem = Instantiate(_prefab, position, Quaternion.identity) Public void SpawnNew(WeaponSO weapon, Vector3 position) If (_instance != null & _instance != this) Public static WeaponItemSpawner Instance => _instance Private static WeaponItemSpawner _instance Public class WeaponItemSpawner : MonoBehaviour We are going to do a simple Singleton to helps us with this: using UnityEngine We are going to use this prefab to spawn it later.įinally we need a way of spawning this weapons in the scene. This is the final gameobject’s structure:ĭrag this WeaponItem gameobject to the prefabs folder to create a prefab of it. Set the collider to be a trigger and freeze all rotations and positions in the Rigidbody Attach the WeaponItem script and also add a Collider (a Sphere collider in my case) and a Rigidbody. (_weapon) ĭrag a weapon sprite into the scene. If (("Player") & other.TryGetComponent(out PlayerStateMachine stateMachine)) Private void OnTriggerEnter(Collider other) _rigidbody.AddForce((Vector3.up + Vector3.right) * force, ForceMode.Impulse) Public void CreateInstance(WeaponSO weapon, float force) _spriteRenderer.sprite = _weapon.WeaponSprite This will help to attach any weapon to the player when touched. Now we need to create a script that will help putting this weapon data into the scene. Drag the previously made prefab (Weapon 1 above) in the Prefab field and adjust the other values to your liking. Name it Weapon 1.Īfter that, modify the properties of this weapon data according to your weapon. In the Projects view right click and create a new Weapon data asset. Public Bounds GetBoundsRelativeToPlayer(Transform player) This script will hold each weapon data and will also help calculate the bounds of an attack relative to the player’s position. Now let’s create a scriptable object that holds our weapon data. Repeat the same steps for each weapon you have. Drag the roots gameobject (Weapon 1) to the Prefabs folder to create a prefab of this weapon. In our case the sword was only 1 sprite but this is useful if we need to add more sprites or even a TrailRenderer to follow the movement. Create 2 nested gameobjects inside to put a Sprites holder and inside this one the actual sprites of the weapon. Create an empty gameobject and name it Weapon 1. We need to create a weapon game object as a visual of to show the players. I made sure to set the pivot somewhere in the handle of each sprite (the little blue circle in the handle you can see on the sprite to the left) For this case I’m going to use this 2 different sprites. The first thing we need is some weapon sprites. If you want to follow along the series you need to first read Ground Zero, Part 1, Part 2, Part 3, Part 4 and Part 5. This is the fifth tutorial of the making of the Cult of the Lamb mechanics in Unity.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |