Well, I’m having a problem with my player’s animation running.
I developed this script that controls practically everything related to the player (don’t be ashamed to get it yourself, the code is free), it handled things correctly for its given instance, I don’t think the problem is with it, but anyway, the transitions of the Animation occurs in the editor for the candles to happen, but in the game scene no, the player (of the Man type) does not execute the animation, no matter what it is, unlike the player (of the woman type) which works correctly. in a previous scene this is the customization scene that defines the parameters and what things will be taken or placed in the player, and that’s where I found this error, because when I stopped focusing on the woman and focused on the man, the bug occurred, although previously this bug did not exist and both worked correctly, please help me, as not even GPT was able to handle the service.
I’m sorry if the code language is different, as I’m Brazilian and the game will be released for a Brazilian target audience.
usando o sistema;
usando System.Collections;
usando System.Collections.Generic;
usando UnityEngine.EventSystems;
em uso de TMPro;
usando UnityEngine;
usando UnityEngine.UI;
usando UnityEngine.SceneManagement;
usando Random = UnityEngine.Random;
usando Unity.VisualScripting;
public class JogadorControlador : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
[cabeçalho("Base")]
público FixedJoystick joyMovimento;
público JogadorPrefabCriador jogadorData;
público InimigoPrefabCriador[] InimigoData;
jogador público GameObject;
público GameObject[] Maos;
público Vector3 Direcoes;
controlador privado CharacterController;
público Avatar JogadorAvatar;
câmera pública principalCamera;
animador público animador;
flutuação pública;
corda pública ProximaCena;
[Cabeçalho("Verificação de Solo")]
bool isGrounded;
público Transformar ground_check;
ground_distance de flutuação pública = 0,4f;
ground_mask pública LayerMask;
[cabeçalho("Estatisticas")]
público TextMeshProUGUI jogadorNome;
carro alegórico públicoVelocidade;
carro alegórico públicoVelocidadePadrao;
carro alegórico público;
carro alegórico públicoForcaMin, jogadorForcaMax;
carro alegórico públicoDefesa;
carro alegórico público;
carro alegórico públicoVidaRemanecente;
carro alegórico público;
carro alegórico públicoEstaminaRemanecente;
carro alegórico públicoExperiência;
público bool jogadorMasculino, jogadoraFemenino;
público JogadorPrefabCriador[] jogadorPrefab;
[Header("Barras e botões")]
público Slider BarraDeVida;
público Slider BarraDeEstamina;
público Slider BarraDeExperiencia;
Botão público AtaqueBotão;
Botão público PegarOuAbrirBotao;
bool público regenerarEstamina;
bool público CorrerBotao = falso;
[Cabeçalho("Personalização")]
público GameObject cabeloCurtoHomem, cabeloLongoHomem, barba, bigode;
público GameObject cabeloCurtoMulher, cabeloLongoMulher;
público GameObject JogadorHomem, JogadorMulher;
material público Renderizador[] Pelé;
Renderizador público[] materialCabelo;
public int IDDoJogadorI = 0;
[header("componentes do audio")]
carro alegórico público Cronometro;
público int DubContador;
público int NumeroDeFalas;
público int falaMinima;
público int falaMaxima;
público int TempoFinal;
carro alegórico público TempoCurto, TempoLongo;
[cabeçalho("Audio Aleatorio Do Radio")]
público AudioSource RadioAudioControlador;
público AudioClip[] RadioDubFatAleatorio;
private bool isCoroutineRunning = falso;
void Start()
{
IDDoJogadorI = PropriedadesDaPele.IDDoJogador;
JogadorPrefabCriador jogadorEscolhido = null; // Inicialize a variável fora do loop
for (int i = 0; i < jogadorPrefab.Length; i++)
{
if (jogadorPrefab[i] != null && jogadorPrefab[i].IDDoJogadorI == PropriedadesDaPele.IDDoJogador)
{
jogadorEscolhido = jogadorPrefab[i]; // Atribua o jogadorPrefab correto à variável jogadorEscolhido
break; // Saia do loop, pois já encontrou o jogador correto
}
}
if (jogadorEscolhido != null)
{
// Faça o que for necessário com o jogadorEscolhido
jogadorData = jogadorEscolhido;
}
else
{
// Lide com o caso em que nenhum jogadorPrefab corresponde ao IDDoJogador
Debug.LogError("Nenhum jogadorPrefab correspondente encontrado para o IDDoJogador " + PropriedadesDaPele.IDDoJogador);
}
if (PropriedadesDaPele.indexCabelo != 0 && JogadorHomem == true)
{
switch (PropriedadesDaPele.indexCabelo)
{
case 1:
if(PropriedadesDaPele.indexCabelo == 1)
{
cabeloCurtoHomem.SetActive(true);
cabeloLongoHomem.SetActive(false);
}
break;
case 2:
if (PropriedadesDaPele.indexCabelo == 2)
{
cabeloCurtoHomem.SetActive(false);
cabeloLongoHomem.SetActive(true);
}
break;
}
}
if (PropriedadesDaPele.indexCabelo != 0 && JogadorMulher == true)
{
switch (PropriedadesDaPele.indexCabelo)
{
case 1:
if (PropriedadesDaPele.indexCabelo == 2)
{
cabeloCurtoMulher.SetActive(true);
cabeloLongoMulher.SetActive(false);
}
break;
case 2:
if (PropriedadesDaPele.indexCabelo == 2)
{
cabeloCurtoMulher.SetActive(false);
cabeloLongoMulher.SetActive(true);
}
break;
}
}
if (IDDoJogadorI == 0 || IDDoJogadorI == 1)
{
if (PropriedadesDaPele.indexHomem != 0 && IDDoJogadorI == 0)
{
JogadorHomem.SetActive(true);
JogadorMulher.SetActive(false);
JogadorAvatar = jogadorData.Avatar;
animador.runtimeAnimatorController = jogadorData.ControladorDeAnimacao;
}
else if(PropriedadesDaPele.indexMulher != 0 && IDDoJogadorI == 1)
{
JogadorHomem.SetActive(false);
JogadorMulher.SetActive(true);
JogadorAvatar = jogadorData.Avatar;
animador.runtimeAnimatorController = jogadorData.ControladorDeAnimacao;
}
}
bigode.SetActive(Convert.ToBoolean(PropriedadesDaPele.indexBigode));
barba.SetActive(Convert.ToBoolean(PropriedadesDaPele.indexBarba));
for (int i = 0; i < materialPele.Length; i++)
{
materialPele[i].material.color = PropriedadesDaPele.CorDaPele;
}
for (int i = 0; i < materialCabelo.Length; i++)
{
materialCabelo[i].material.color = PropriedadesDaPele.CorDoCabelo;
}
controlador = GetComponent<CharacterController>();
animador = GetComponent<Animator>();
mainCamera = Camera.main;
regenerarEstamina = false;
RadioAudioControlador = GetComponent<AudioSource>();
RadioAudioControlador.ignoreListenerPause = true;
ChamarCoroutine();
BarraDeVida.value = jogadorVida;
BarraDeEstamina.value = jogadorEstamina;
animador.avatar = jogadorData.Avatar;
jogadorNome = PropriedadesDaPele.NomeDoJogador;
animador.runtimeAnimatorController = jogadorData.ControladorDeAnimacao;
jogador = jogadorData.JogadorObjeto;
jogadorNome = jogadorData.Nome;
jogadorMasculino = jogadorData.Masculino;
jogadoraFemenino = jogadorData.Femenino;
jogadorVelocidade = jogadorData.Velocidade;
jogadorVelocidadePadrao = jogadorData.VelocidadePadrao;
correr = jogadorData.CorreVelocidade;
jogadorVida = jogadorData.Vida;
jogadorVidaRemanecente = jogadorData.VidaRemanecente;
jogadorEstamina = jogadorData.Estamina;
jogadorEstaminaRemanecente = jogadorData.EstaminaRemanecente;
jogadorForcaMin = jogadorData.ForcaMin;
jogadorForcaMax = jogadorData.ForcaMax;
jogadorDefesa = jogadorData.Defesa;
jogadorExperiencia = jogadorData.Experiencia;
}
void Update()
{
MovimentacaoPorJoy();
RegenerarEstaminaEVida();
ElevarExperiencia();
Morte();
}
void MovimentacaoPorJoy()
{
if (isGrounded && Direcoes.y < 0)
{
Direcoes.y = -2f;
}
else
{
Direcoes.y += gravidade * Time.deltaTime;
}
Vector3 cameraForward = mainCamera.transform.forward;
Vector3 cameraRight = mainCamera.transform.right;
cameraForward.y = 0f;
cameraRight.y = 0f;
float horizontalInputJoy = joyMovimento.Horizontal;
float verticalInputJoy = joyMovimento.Vertical;
Vector3 movimentoDirecaoJoy = (cameraForward * verticalInputJoy + cameraRight * horizontalInputJoy);
if (movimentoDirecaoJoy != Vector3.zero)
{
// Movimentar o jogador na direção desejada
Vector3 movimento = movimentoDirecaoJoy.normalized * jogadorVelocidade * Time.deltaTime;
controlador.Move(movimento);
// Definir as animações de movimento
animador.SetFloat("Horizontal", horizontalInputJoy);
animador.SetFloat("Vertical", verticalInputJoy);
// Orientar o objeto na direção do movimento
if (movimentoDirecaoJoy.magnitude > 0.1f) // Garante que o movimentoDirecaoJoy não seja muito pequeno
{
Quaternion novaRotacao = Quaternion.LookRotation(cameraForward);
transform.rotation = Quaternion.Slerp(transform.rotation, novaRotacao, Time.deltaTime * 10f);
}
}
else
{
// Se não houver movimento, definir as animações de movimento para zero
animador.SetFloat("Horizontal", 0);
animador.SetFloat("Vertical", 0);
}
if(jogadorVelocidade >= correr && (horizontalInputJoy >= 0.1f || horizontalInputJoy <= 0.1f || verticalInputJoy >= 0.1 || verticalInputJoy <= 0.1))
{
animador.SetBool("Correr", true);
jogadorEstamina -= 10f * Time.deltaTime;
}
else if(jogadorVelocidade <= jogadorVelocidadePadrao)
{
animador.SetBool("Correr", false);
}
}
public void CorrerAtivar()
{
CorrerBotao = true;
if (CorrerBotao && jogadorVelocidade != 0)
{
jogadorVelocidade = correr;
}
}
public void CorrerDesativar()
{
CorrerBotao = false;
if (!CorrerBotao)
{
jogadorVelocidade = jogadorVelocidadePadrao;
}
}
public void OnPointerDown(PointerEventData eventData)
{
CorrerBotao = true;
}
public void OnPointerUp(PointerEventData eventData)
{
CorrerBotao = false;
}
void ElevarExperiencia()
{
BarraDeExperiencia.value = jogadorExperiencia;
}
void RegenerarEstaminaEVida()
{
BarraDeVida.value = jogadorVida;
BarraDeEstamina.value = jogadorEstamina;
if (BarraDeEstamina.value <= 100 && !regenerarEstamina)
{
regenerarEstamina = true;
StartCoroutine(RegenerarEstaminaFuncao());
}
}
public void Socar()
{
if (AtaqueBotao == true)
{
if (jogadorEstamina >= 30)
{
animador.SetBool("Socar", true);
jogadorEstamina -= Random.Range(20f, 30f);
jogadorExperiencia += Random.Range(1f, 3.5f);
for (int i = 0; i < Maos.Length; i++)
{
if (Maos[i] != null)
{
Maos[i].SetActive(true);
}
}
}
else
{
for (int i = 0; i < Maos.Length; i++)
{
if (Maos[i] != null)
{
Maos[i].SetActive(false);
}
}
animador.SetBool("Socar", false);
}
StartCoroutine(PararDeSocar());
}
}
IEnumerator PararDeSocar()
{
yield return new WaitForSeconds(0.5f);
for (int i = 0; i < Maos.Length; i++)
{
if (Maos[i] != null)
{
Maos[i].SetActive(false);
}
}
animador.SetBool("Socar", false);
}
IEnumerator RegenerarEstaminaFuncao()
{
while (jogadorEstamina < 100)
{
yield return new WaitForSeconds(0.5f);
jogadorEstamina += Random.Range(0, jogadorEstaminaRemanecente);
// Garante que a estamina não ultrapasse 100
jogadorEstamina = Mathf.Min(jogadorEstamina, 100);
// Atualiza a BarraDeEstamina
BarraDeEstamina.value = jogadorEstamina;
}
regenerarEstamina = false;
}
void OnApplicationFocus(bool hasFocus)
{
if (hasFocus)
{
ChamarCoroutine();
}
}
public void ChamarCoroutine()
{
if (!isCoroutineRunning)
{
StartCoroutine(RadioRotina());
}
}
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "ColisorDeAtaque")
{
for (int i = 0; i < InimigoData.Length; i++)
{
jogadorVida -= Random.Range(InimigoData[i].ForcaMin - jogadorDefesa, InimigoData[i].ForcaMax - jogadorDefesa);
}
}
}
public IEnumerator RadioRotina()
{
isCoroutineRunning = true;
while (true)
{
if (DubContador < RadioDubFatAleatorio.Length)
{
RadioAudioControlador.clip = RadioDubFatAleatorio[DubContador];
RadioAudioControlador.Play();
// Aguarde a duração da faixa de áudio atual
yield return new WaitForSeconds(RadioAudioControlador.clip.length);
}
else
{
Debug.LogError("Índice fora do alcance para 'RadioAleatorio'.");
RadioAudioControlador.enabled = false;
isCoroutineRunning = false;
yield break; // Saia da coroutine se o índice estiver fora do alcance.
}
Cronometro = TempoFinal; // Reinicie o cronômetro para o próximo ciclo.
int incrementoAleatorio = Random.Range(falaMinima, falaMaxima + 1);
DubContador += incrementoAleatorio;
if (DubContador >= NumeroDeFalas || DubContador >= RadioDubFatAleatorio.Length)
{
DubContador = 0;
}
// Aguarde um curto período antes de iniciar a próxima faixa de áudio
yield return new WaitForSeconds(Random.Range(TempoCurto, TempoLongo));
}
}
void Morte()
{
if (jogadorVida <= 0f)
{
animador.SetBool("Morte", true);
StartCoroutine(DestruirObjeto());
}
}
IEnumerator DestruirObjeto()
{
yield return new WaitForSeconds(5f);
SceneManager.LoadScene(ProximaCena);
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PropriedadesJogador : MonoBehaviour
{
[Header("base")]
public Slider rodar;
public string ProximaCena;
public TextMeshProUGUI nome;
public GameObject NomePainel;
public GameObject HomemPainel, MulherPainel;
private int IndexCabelo, IndexBigode, IndexBarba, IndexHomem, IndexMulher;
public int IDDoJogadorJ;
public Avatar AvatarJogadorHomem, AvatarJogadorMulher;
public RuntimeAnimatorController AnimatorHomem, AnimatorMulher;
[Header("Homem")]
public GameObject cabeloCurtoHomem, cabeloLongoHomem, barba, bigode;
public Color CorDaPeleHomem;
public Color CorDoCabeloEBarba;
public Slider Rhomem, Ghomem, Bhomem;
public Slider Rcbhomem, Gcbhomem, Bcbhomem;
public Renderer PeleHomem;
public JogadorPrefabCriador PropriedadesHomem;
public Renderer[] MaterialCabeloHomem;
public GameObject JogadorHomem;
[Header("Mulher")]
public GameObject cabeloCurtoMulher, cabeloLongoMulher;
public Color CorDaPeleMulher;
public Color CorDoCabelo;
public Slider Rmulher, Gmulher, Bmulher;
public Slider Rcbmulher, Gcbmulher, Bcbmulher;
public Renderer PeleMulher;
public GameObject JogadorMulher;
public Renderer[] MaterialCabeloMulher;
public JogadorPrefabCriador PropriedadesMulher;
void Start()
{
rodar.value = 0.5f;
cabeloCurtoHomem = GameObject.FindWithTag("CabeloCurtoHomem");
cabeloCurtoMulher = GameObject.FindWithTag("CabeloCurtoMulher");
cabeloLongoHomem = GameObject.FindWithTag("CabeloLongoHomem");
cabeloLongoMulher = GameObject.FindWithTag("CabeloLongoMulher");
bigode = GameObject.FindWithTag("Bigode");
barba = GameObject.FindWithTag("Barba");
JogadorHomem = GameObject.FindWithTag("JogadorHomem");
JogadorMulher = GameObject.FindWithTag("JogadorMulher");
PropriedadesDaPele.Homem = PropriedadesHomem.Masculino;
PropriedadesDaPele.Mulher = PropriedadesMulher.Femenino;
JogadorHomem.SetActive(false);
JogadorMulher.SetActive(false);
HomemPainel.SetActive(false);
MulherPainel.SetActive(false);
cabeloCurtoHomem.SetActive(false);
cabeloCurtoMulher.SetActive(false);
cabeloLongoHomem.SetActive(false);
cabeloLongoMulher.SetActive(false);
bigode.SetActive(false);
barba.SetActive(false);
}
void Update()
{
JogadorHomem.transform.rotation = Quaternion.Euler(0, rodar.value, 0);
JogadorMulher.transform.rotation = Quaternion.Euler(0, rodar.value, 0);
CorDaPeleHomem.r = Rhomem.value;
CorDaPeleHomem.g = Ghomem.value;
CorDaPeleHomem.b = Bhomem.value;
CorDoCabeloEBarba.r = Rcbhomem.value;
CorDoCabeloEBarba.g = Gcbhomem.value;
CorDoCabeloEBarba.b = Bcbhomem.value;
CorDaPeleMulher.r = Rmulher.value;
CorDaPeleMulher.g = Gmulher.value;
CorDaPeleMulher.b = Bmulher.value;
CorDoCabelo.r = Rcbmulher.value;
CorDoCabelo.g = Gcbmulher.value;
CorDoCabelo.b = Bcbmulher.value;
PeleHomem.material.color = CorDaPeleHomem;
PeleMulher.material.color = CorDaPeleMulher;
for (int i = 0; i < MaterialCabeloHomem.Length; i++)
{
MaterialCabeloHomem[i].material.color = CorDoCabeloEBarba;
}
for (int i = 0; i < MaterialCabeloMulher.Length; i++)
{
MaterialCabeloMulher[i].material.color = CorDoCabelo;
}
}
public void SemCabeloHomem()
{
cabeloCurtoHomem.SetActive(false);
cabeloLongoHomem.SetActive(false);
IndexCabelo = 0;
}
public void SemCabeloMulher()
{
cabeloCurtoMulher.SetActive(false);
cabeloLongoMulher.SetActive(false);
IndexCabelo = 0;
}
public void CabeloCurtoHomem()
{
cabeloCurtoHomem.SetActive(true);
cabeloLongoHomem.SetActive(false);
IndexCabelo = 1;
}
public void CabeloCurtoMulher()
{
cabeloCurtoMulher.SetActive(true);
cabeloLongoMulher.SetActive(false);
IndexCabelo = 1;
}
public void CabeloLongoHomem()
{
cabeloCurtoHomem.SetActive(false);
cabeloLongoHomem.SetActive(true);
IndexCabelo = 2;
}
public void CabeloLongoMulher()
{
cabeloCurtoMulher.SetActive(false);
cabeloLongoMulher.SetActive(true);
IndexCabelo = 2;
}
public void Barba()
{
barba.SetActive(true);
IndexBarba = 1;
}
public void Bigode()
{
bigode.SetActive(true);
IndexBigode = 1;
}
public void SemNada()
{
bigode.SetActive(false);
barba.SetActive(false);
IndexBigode = 0;
IndexBarba = 0;
}
public void ResetarCorDaPeleHomem()
{
Rhomem.value = 1.1f;
Rhomem.value = 1.1f;
Rhomem.value = 1.1f;
}
public void ResetarCorDaPeleMulher()
{
Rmulher.value = 1.1f;
Gmulher.value = 1.1f;
Bmulher.value = 1.1f;
}
public void ResetarCorDoCabeloHomem()
{
Rcbhomem.value = 1.1f;
Gcbhomem.value = 1.1f;
Bcbhomem.value = 1.1f;
}
public void ResetarCorDoCabeloMulher()
{
Rcbmulher.value = 1.1f;
Gcbmulher.value = 1.1f;
Bcbmulher.value = 1.1f;
}
public void HomemSeletor()
{
JogadorHomem.SetActive(true);
JogadorMulher.SetActive(false);
MulherPainel.SetActive(false);
HomemPainel.SetActive(true);
PropriedadesDaPele.Homem = PropriedadesHomem;
PropriedadesDaPele.AvatarJogadorHomem = AvatarJogadorHomem;
PropriedadesDaPele.AnimadorHomem = AnimatorHomem;
IndexHomem = 1;
IndexMulher = 0;
IDDoJogadorJ = 0;
}
public void MulherSeletor()
{
JogadorMulher.SetActive(true);
JogadorHomem.SetActive(false);
MulherPainel.SetActive(true);
HomemPainel.SetActive(false);
PropriedadesDaPele.Mulher = PropriedadesMulher;
PropriedadesDaPele.AvatarJogadorMulher = AvatarJogadorMulher;
PropriedadesDaPele.AnimadorMulher = AnimatorMulher;
IndexHomem = 0;
IndexMulher = 1;
IDDoJogadorJ = 1;
}
public void Confirmar()
{
PropriedadesDaPele.indexCabelo = IndexCabelo;
PropriedadesDaPele.indexBigode = IndexBigode;
PropriedadesDaPele.indexBarba = IndexBarba;
PropriedadesDaPele.indexHomem = IndexHomem;
PropriedadesDaPele.indexMulher = IndexMulher;
PropriedadesDaPele.CorDaPele = CorDaPeleHomem;
PropriedadesDaPele.CorDaPele = CorDaPeleMulher;
PropriedadesDaPele.CorDoCabelo = CorDoCabeloEBarba;
PropriedadesDaPele.CorDoCabelo = CorDoCabelo;
PropriedadesDaPele.Homem = PropriedadesHomem;
PropriedadesDaPele.Mulher = PropriedadesMulher;
PropriedadesDaPele.NomeDoJogador = nome;
PropriedadesDaPele.IDDoJogador = IDDoJogadorJ;
PropriedadesDaPele.AvatarJogadorMulher = AvatarJogadorMulher;
PropriedadesDaPele.AvatarJogadorHomem = AvatarJogadorHomem;
NomePainel.SetActive(true);
}
public void Mundo()
{
SceneManager.LoadScene(ProximaCena);
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using TMPro;
[CreateAssetMenu(fileName = "Jogador", menuName = "TGM/Novo Jogador")]
public class JogadorPrefabCriador : ScriptableObject
{
[Header("Base")]
public GameObject JogadorObjeto;
public RuntimeAnimatorController ControladorDeAnimacao;
public Avatar Avatar;
public AudioClip[] JogadorAudio;
public JogadorPrefabCriador Jogador;
public int IDDoJogadorI;
[Header("Atributos")]
public TextMeshProUGUI Nome;
public float VelocidadePadrao;
public float Velocidade;
public float CorreVelocidade;
public float ForcaMin, ForcaMax;
public float DanoCriticoMax;
public float Defesa;
public float Vida;
public float VidaRemanecente;
public float Estamina;
public float Experiencia;
public float EstaminaRemanecente;
public bool Masculino, Femenino;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PropriedadesDaPele : MonoBehaviour
{
public static int indexCabelo;
public static int indexBigode;
public static int indexBarba;
public static int indexHomem;
public static int indexMulher;
public static int IDDoJogador;
public static Color CorDaPele;
public static Color CorDoCabelo;
public static TextMeshProUGUI NomeDoJogador;
public static bool Homem, Mulher;
public static PropriedadesJogador PropriedadesMulher1, PropriedadesHomem1;
public static Avatar AvatarJogadorHomem, AvatarJogadorMulher;
public static RuntimeAnimatorController AnimadorHomem, AnimadorMulher;
}
Here are the images with more data
The first script refers to the player.
The second script refers to the customization scene
The third script is the data for each type of player (Man and woman.)
The fourth script is the data saved from the customization area to be used in the game scene, which is where the player moves and everything…
You can also see that one of the images is missing the controller and the avatar, this is because the first script will define this based on the others, which normally happens.