If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.
My game is very broken at this stage. Besides movement and power attacks nothing appears to be working. In your game you can attack, working enemy hp bar and make player and enemy face each other at all time but despite following this my basic attacks are not working and the enemy hp bar won’t be updated. I think most of the problems come from this script: using System.Collections;
using UnityEngine;
using UnityEngine.Assertions;
namespace RPG.Characters {
public class WeaponSystem : MonoBehaviour {
[SerializeField] float baseDamage = 15f;
[SerializeField] WeaponConfig currentWeaponConfig;
GameObject target;
GameObject weaponObject;
Animator animator;
Character character;
float lastHitTime = 0f;
const string ATTACK_TRIGGER = "Attack";
const string DEFAULT_ATTACK = "DEFAULT_ATTACK";
void Start () {
PutWeaponInHand (currentWeaponConfig);
SetAttackAnimation ();
animator = GetComponent<Animator> ();
character = GetComponent<Character> ();
}
void Update () {
bool targetIsDead;
bool targetIsOutOfRange;
if (target == null) {
targetIsOutOfRange = false;
targetIsDead = false;
} else {
var targethealth = target.GetComponent<HealthSystem> ().healthAsPercentage;
targetIsDead = targethealth <= Mathf.Epsilon;
var distanceToTarget = Vector3.Distance (transform.position, target.transform.position);
targetIsOutOfRange = distanceToTarget > currentWeaponConfig.GetMaxAttackRange ();
}
float characterHealth = GetComponent<HealthSystem> ().healthAsPercentage;
bool characterIsDead = (characterHealth <= Mathf.Epsilon);
if (characterIsDead || targetIsOutOfRange || targetIsDead) {
StopAllCoroutines ();
}
}
private void SetAttackAnimation () {
if (!character.GetOverrideController ()) {
Debug.Break ();
Debug.LogAssertion ("Please prove " + gameObject + " with an animator override controller.");
} else {
var animatorOverrideController = character.GetOverrideController ();
animator.runtimeAnimatorController = animatorOverrideController;
animatorOverrideController[DEFAULT_ATTACK] = currentWeaponConfig.GetAnimClip(); //TODO remove default attack constant
}
}
private GameObject RequestDominantHand () {
var dominantHands = GetComponentsInChildren<DominantHand> ();
int numberOfDominantHands = dominantHands.Length;
Assert.IsFalse(numberOfDominantHands <= 0, "No DominantHand found on Player, please add one");
Assert.IsFalse(numberOfDominantHands > 1, "Multiple DominantHand scripts on Player, please remove one");
return dominantHands[0].gameObject;
}
private float CalculateDamage() {
return baseDamage;
}
public void AttackTarget(GameObject targetToAttack) {
target = targetToAttack;
StartCoroutine (AttackTargetRepeatedly ());
}
public void StopAttacking() {
StopAllCoroutines ();
}
IEnumerator AttackTargetRepeatedly() {
bool attackerStillAlive = GetComponent<HealthSystem>().healthAsPercentage >= Mathf.Epsilon;
bool targetStillAlive = target.GetComponent<HealthSystem> ().healthAsPercentage >= Mathf.Epsilon;
while (attackerStillAlive && targetStillAlive) {
float weaponHitPeriod = currentWeaponConfig.GetHitInterval ();
float timeToWait = weaponHitPeriod * character.GetAnimSpeedMultiplier ();
bool isTimeToHitAgain = Time.time - lastHitTime > timeToWait;
if (isTimeToHitAgain) {
AttackTargetOnce();
lastHitTime = Time.time;
}
yield return new WaitForSeconds (timeToWait);
}
}
void AttackTargetOnce() {
transform.LookAt(target.transform);
animator.SetTrigger(ATTACK_TRIGGER);
float damageDelay = .5f; //todo get from other area
SetAttackAnimation();
StartCoroutine (DamageAfterDelay (damageDelay));
}
IEnumerator DamageAfterDelay(float delay) {
yield return new WaitForSecondsRealtime (delay);
target.GetComponent<HealthSystem>().TakeDamage(CalculateDamage());
}
public WeaponConfig GetCurrentWeapon() {
return currentWeaponConfig;
}
private void AttackTarget() {
if (Time.time - lastHitTime > currentWeaponConfig.GetHitInterval()) {
SetAttackAnimation ();
animator.SetTrigger (ATTACK_TRIGGER);
lastHitTime = Time.time;
}
}
public void PutWeaponInHand(WeaponConfig weaponToUse) {
currentWeaponConfig = weaponToUse;
GameObject dominantHand = RequestDominantHand ();
var weaponPrefab = weaponToUse.GetWeaponPrefab ();
Destroy (weaponObject); //empty hands
weaponObject = Instantiate (weaponPrefab,dominantHand.transform);
weaponObject.transform.localPosition = currentWeaponConfig.gripTransform.localPosition;
weaponObject.transform.localRotation = currentWeaponConfig.gripTransform.localRotation;
}
}
}
I’m getting many nullreferenceexceptions from it