Hello! I was following a game tutorial on udemy but the course is deleted. The game consists of a platform with 4 Ai and 1 player where the player and Ai punches each other to fight and get their respective enemies out of the platform to gain score. My problem is, when the Ai punches another Ai or the main player, the speed of Ai or main player becomes zero and they don’t move. Can anyone please help me with this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CnControls;
using DG.Tweening;
using TMPro;
using UnityEngine.AI;
public class easyplayer : MonoBehaviour
{
public bool active;
public bool isAi;
public Transform[] bones;
public bool keepBonesStraight;
public float speed;
public float turnSpeed;
public bool isInZone;
public int score;
public Transform scoreFx;
public Transform zoneEasy;
public Transform target;
public float outsideOfZoneRange;
public ParticleSystem easytrailFx;
public bool canPunch = true;
public float punchForce;
public GameObject punchFx;
public bool stunned;
public Transform puncher;
public Transform scoreCard;
public TextMeshPro playerName;
public GameObject crown;
private Rigidbody rb;
private gamemanager gamemanagerScript;
private NavMeshAgent nav;
private zoneEasy zoneEasyScript;
private float defaultSpeed;
private Animator easyanim;
public AudioSource punchSfx;
void Start()
{
rb = GetComponent<Rigidbody>();
gamemanagerScript = GameObject.FindWithTag("easymanager").GetComponent<gamemanager>();
easyanim = GetComponent<Animator>();
InvokeRepeating("addZoneScore", 1, 1);
if (isAi)
{
nav = GetComponent<NavMeshAgent>();
zoneEasyScript = zoneEasy.GetComponent<zoneEasy>();
}
else
{
defaultSpeed = speed;
}
}
private void OnEnable()
{
if (active)
{
canPunch = true;
stunned = false;
ParticleSystem.EmissionModule em = easytrailFx.emission;
em.enabled = true;
easyanim.SetBool("eRun", true);
rb.velocity = Vector3.zero;
if (isAi)
nav.enabled = true;
else
{
// camFollowScript.enabled = true;
speed = defaultSpeed;
}
// if (isBomber)
// unstable = false;
Vector3 dir = new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(zoneEasy.position.x, 0, zoneEasy.position.z);
transform.rotation = Quaternion.LookRotation(-dir);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.LoadLevel("Title");
}
if (gamemanagerScript.gameStarted && !active)
{
active = true;
easyanim.SetBool("eRun", true);
ParticleSystem.EmissionModule em = easytrailFx.emission;
em.enabled = true;
if (isAi)
nav.enabled = true;
}
if (active)
{
if (isAi)
{
if (!stunned)
{
if (gamemanagerScript.playersInZone.Contains(transform))
{
target = getClosestEnemyInZone(gamemanagerScript.playersInZone);
if (target == null)
target = zoneEasyScript.wayPoints[Random.Range(0, zoneEasyScript.wayPoints.Length)];
}
else
{
target = getClosestEnemy(gamemanagerScript.players);
if (Vector3.Distance(transform.position, target.position) > outsideOfZoneRange)
target = zoneEasy;
}
nav.SetDestination(target.position);
}
}
else
{
if (Input.GetMouseButton(0))
{
Vector3 touchMagnitude = new Vector3(CnInputManager.GetAxis("Horizontal"), CnInputManager.GetAxis("Vertical"), 0);
Vector3 touchPos = transform.position + touchMagnitude.normalized;
Vector3 touchDir = touchPos - transform.position;
float angle = Mathf.Atan2(touchDir.y, touchDir.x) * Mathf.Rad2Deg;
angle -= 90;
Quaternion rot = Quaternion.AngleAxis(angle, Vector3.down);
transform.rotation = Quaternion.Lerp(transform.rotation, rot, turnSpeed * Mathf.Min(Time.deltaTime, .04f));
}
}
/* if (transform.position.y < -1 && !stunned)
{
ParticleSystem.EmissionModule em = easytrailFx.emission;
em.enabled = false;
stunned = true;
speed = 0;
StartCoroutine(death());
}*/
}
}
private void FixedUpdate()
{
if (active && !isAi)
rb.MovePosition(transform.position + transform.forward * speed * Time.deltaTime);
}
private void LateUpdate()
{
if (keepBonesStraight)
foreach (Transform t in bones)
t.eulerAngles = new Vector3(0, t.eulerAngles.y, t.eulerAngles.z);
}
private void addZoneScore()
{
if (isInZone)
{
if (isAi)
{
score++;
}
else
{
score++;
Transform t = Instantiate(scoreFx, new Vector3(transform.position.x, transform.position.y + 2, transform.position.z), Quaternion.identity);
TextMeshPro txt = t.GetComponent<TextMeshPro>();
txt.DOFade(0, .5f).SetDelay(.5f);
t.DOMoveY(t.position.y + 2, 1);
Destroy(t.gameObject, 2);
}
}
}
public void addKnockOutScore()
{
if (isInZone)
{
if (isAi)
{
score += 4;
}
else
{
score += 4;
Transform t = Instantiate(scoreFx, new Vector3(transform.position.x, transform.position.y + 2, transform.position.z), Quaternion.identity);
TextMeshPro txt = t.GetComponent<TextMeshPro>();
txt.text = "+4";
txt.color = Color.yellow;
txt.DOFade(0, .5f).SetDelay(.5f);
t.DOMoveY(t.position.y + 2, 1);
t.DOPunchScale(new Vector3(.5f, .5f, .5f), .8f);
Destroy(t.gameObject, 2);
}
}
}
private Transform getClosestEnemyInZone(List<Transform> enemies)
{
Transform bestTarget = null;
float smallestDistance = Mathf.Infinity;
Vector3 currentPosition = transform.position;
foreach (Transform potientialTarget in enemies)
{
if (potientialTarget != transform)
{
Vector3 directionToTarget = potientialTarget.position - currentPosition;
float distance = directionToTarget.sqrMagnitude;
if (distance < smallestDistance)
{
smallestDistance = distance;
bestTarget = potientialTarget;
}
}
}
return bestTarget;
}
private Transform getClosestEnemy(easyplayer[] enemies)
{
Transform bestTarget = null;
float smallestDistance = Mathf.Infinity;
Vector3 currentPosition = transform.position;
foreach (easyplayer potientialTarget in enemies)
{
if (potientialTarget.transform != transform)
{
Vector3 directionToTarget = potientialTarget.transform.position - currentPosition;
float distance = directionToTarget.sqrMagnitude;
if (distance < smallestDistance)
{
smallestDistance = distance;
bestTarget = potientialTarget.transform;
}
}
}
return bestTarget;
}
public void punch(Transform other)
{
if (canPunch)
{
canPunch = false;
easyanim.SetBool("eAttack", true);
StartCoroutine(resetPunch());
easyplayer tempeasyplayerScript = other.GetComponent<easyplayer>();
tempeasyplayerScript.puncher = transform;
tempeasyplayerScript.StartCoroutine(tempeasyplayerScript.stun());
Rigidbody _rb = other.GetComponent<Rigidbody>();
_rb.velocity = transform.forward * punchForce;
}
}
private IEnumerator resetPunch()
{
yield return new WaitForSeconds(.1f);
keepBonesStraight = false;
punchFx.SetActive(true);
yield return new WaitForSeconds(.25f);
keepBonesStraight = true;
punchFx.SetActive(false);
canPunch = true;
easyanim.SetBool("eAttack", false);
}
public IEnumerator stun()
{
stunned = true;
ParticleSystem.EmissionModule em = easytrailFx.emission;
em.enabled = false;
if (isAi)
{
canPunch = false;
nav.enabled = false;
punchSfx.Play();
}
else
{
speed = 0;
}
yield return new WaitForSeconds(.25f);
if (Vector3.Distance(Vector3.zero, new Vector3(transform.position.x, 0, transform.position.z)) < 23f)
{
rb.velocity = Vector3.zero;
stunned = false;
em.enabled = true;
if (isAi)
{
canPunch = true;
nav.enabled = true;
}
else
{
speed = defaultSpeed;
}
}
else
{
if (puncher)
{
puncher.GetComponent<easyplayer>().addKnockOutScore();
puncher = null;
}
StartCoroutine(death());
}
}
private IEnumerator death()
{
yield return null;
}
public void stop(bool won)
{
active = false;
ParticleSystem.EmissionModule em = easytrailFx.emission;
em.enabled = false;
easyanim.SetBool("eRun", false);
if (isAi)
nav.enabled = false;
else
speed = 0;
if (won)
{
easyanim.SetBool("eWon", true);
}
else
{
easyanim.SetBool("eLost", true);
}
}
}