Really strange behavior! Bug! Don't know what's wrong

So the animator sometimes works and sometimes it doesn’t. And it also does this weird thing where it will just turn around to point in this one direction. Not sure why. I looked through the code, I couldn’t find it. I compared it to the gitlab repo. I couldn’t find the issue.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShootAction : BaseAction
{
	private enum State 
	{
		Aiming,
		Shooting,
		Cooloff
	}
	
	public event EventHandler OnShoot;

	[SerializeField] private int maxShootDistance;
	[SerializeField] private float shootingStateTime = .1f;
	[SerializeField] private float coolOffStateTime = .5f;
	[SerializeField] private float rotateSpeed = 10f;
	
	private State state;
	private float stateTimer;
	private Unit targetUnit;
	private bool canShootBullet;
	
	
	private void Update()
	{
		if (!isActive) return;
		
		stateTimer -= Time.deltaTime;
		
		switch (state)
		{
		
		case State.Aiming:
			Vector3 aimDirection = (targetUnit.GetWorldPosition() - unit.GetWorldPosition()).normalized;
				transform.forward = Vector3.Lerp(transform.forward, aimDirection, rotateSpeed * Time.deltaTime);
				break;
			case State.Shooting:
				if (canShootBullet)
				{
					Shoot();
					canShootBullet = false;
				}
				break;
			case State.Cooloff:
				break;
		}
		
		if (stateTimer <= 0f)
		{
			NextState();
		}
	}
	
	private void NextState()
	{
		switch (state)
		{
			case State.Aiming:
				state = State.Shooting;
				stateTimer = shootingStateTime;
				break;
			case State.Shooting:
				state = State.Cooloff;
				stateTimer = coolOffStateTime;
				break;
			case State.Cooloff:
				ActionComplete();
				break;
		}
		
		Debug.Log(state);
	}
	
	private void Shoot()
	{
		OnShoot?.Invoke(this, EventArgs.Empty);
		targetUnit.Damage();
	}
	
	public override string GetActionName() 
	{
		return "Shoot";
	}
	
	public override List<GridPosition> GetValidActionGridPositionList()
	{
		List<GridPosition> validGridPositionList = new List<GridPosition>();
		
		GridPosition unitGridPosition = unit.GetGridPosition();
		
		for (int x = -maxShootDistance; x <= maxShootDistance; x++)
		{
			for (int z = -maxShootDistance; z <= maxShootDistance; z++)
			{
				GridPosition offsetGridPosition = new GridPosition(x, z);
				GridPosition testGridPosition = unitGridPosition + offsetGridPosition;
				
				if(!LevelGrid.Instance.IsValidGridPosition(testGridPosition))
				{
					continue;
				}
				
				int testDistance = Mathf.Abs(x) + Mathf.Abs(z);
				
				if (testDistance > maxShootDistance)
				{
					continue;
				}
				
				if (!LevelGrid.Instance.HasAnyUnitOnGridPosition(testGridPosition))
				{
					// Grid Position is empty, no unit
					continue;
				}
				
				Unit targetUnit = LevelGrid.Instance.GetUnitAtGridPosition(testGridPosition);
				
				if (targetUnit.IsEnemy() == unit.IsEnemy())
				{
					// Both Units on same team
					continue;
				}
				
				validGridPositionList.Add(testGridPosition);
			}
		}
		
		return validGridPositionList;
	}
	
	public override void TakeAction(GridPosition gridPosition, Action onActionComplete)
	{
		ActionStart(onActionComplete);
		
		targetUnit = LevelGrid.Instance.GetUnitAtGridPosition(gridPosition);
		
		Debug.Log("Aiming");
		state = State.Aiming;
		float aimingStateTime = 1f;
		stateTimer = aimingStateTime;
		
		canShootBullet = true;
	}
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnitAnimator : MonoBehaviour
{
	[SerializeField] private Animator animator;
	
	private void Awake()
	{
		if (TryGetComponent<MoveAction>(out MoveAction moveAction))
		{
			moveAction.OnStartMoving += MoveAction_OnStartMoving;
			moveAction.OnStopMoving += MoveAction_OnStopMoving;
		}
		
		if (TryGetComponent<ShootAction>(out ShootAction shootAction))
		{
			shootAction.OnShoot += ShootAction_OnShoot;
		}
	}
	
	private void MoveAction_OnStartMoving(object sender, EventArgs e)
	{
		animator.SetBool("isMoving", true);
	}
	
	private void MoveAction_OnStopMoving(object sender, EventArgs e)
	{
		animator.SetBool("isMoving", false);
	}
	
	private void ShootAction_OnShoot(object sender, EventArgs e)
	{
		animator.SetTrigger("shoot");
	}
}

It seems the issue only happens after you move, so I would guess the MoveAction and the ShootAction are both staying active at the same time and both trying to rotate the same object and play different animations.
Check the isActive of both actions, add a Debug.Log(this + " " + isActive) before the return

Also add a Debug.Log when setting the animator trigger. Is it setting the trigger correctly every time?

3 Likes

Wow! That is amazing that you caught that so easily and without even seeing the move code WOW! Uhh yeah that was the issue. I forgot to change onActionComplete() to ActionComplete() in the MoveAction script.

Thank you very much :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms