Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Fiz o Player Correr;

Tô aproveitando o curso pra tentar adicionar algumas outras condições. Fiz o HP dos zumbis serem gerados randomicamente de 1 a 6(excludente), ao ser morto o zumbi soma uma pontuação = a quantidade de vida , que é exibida ao final do jogo ( morrendo ou ganhando). Criei uma condição de vitória que é ativada se o player chegar a um raio de 5 ou menos do centro de um acampamento. Por fim fiz um Script que permite ao jogador correr enquanto segurar o shift, ele também tem um limite de energia, ficou um tanto porco, mas tá cumprindo o seu papel, aparentemente corrigi todos os bugs. Me baseei nos scripts de gerador de Zumbis e controle da arma, esse novo script tá salvo e sendo executado diretamente no prefab do jogador.

Aceito sugestões de melhoria :)

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

public class Correr : MonoBehaviour
{
    public float energia = 3;
    public float contadorTempo ;
    GameObject Player;
    public float VelocidadeCorrendo = 1.5f;
    private bool correndo = false; 
    private bool cansado = false;

    private void Start()
    {
        Player = GameObject.FindWithTag(Tags.player);
    }
    void Update()
    {
        PlayerCorrendo();
        Contador();
        Descansar();
    }
    void PlayerCorrendo()
    {
        if(cansado == false)
        {
            if (Input.GetButtonDown("Fire3"))
            {
                Player.GetComponent<PlayerInput>().StatusPlayer.velocidade *= VelocidadeCorrendo;
                correndo = true;
            }
            else if (Input.GetButtonUp("Fire3"))
            {
                Player.GetComponent<PlayerInput>().StatusPlayer.velocidade /= VelocidadeCorrendo;
                correndo = false;
            }
        }
    }
    void Contador()
    {
        if(correndo == true)
        {
            contadorTempo += Time.deltaTime;
            if (contadorTempo >= energia)
            {
                cansado = true;
                Player.GetComponent<PlayerInput>().StatusPlayer.velocidade /= VelocidadeCorrendo;
                correndo = false;
            }
        }
    }
    void Descansar()
    {
        if(contadorTempo > 0 && Player.GetComponent<PlayerInput>().vetor.magnitude <= 0.1)
        {
            contadorTempo -= (Time.deltaTime / 2);
            if(contadorTempo <= 1)
            {
                cansado = false;
            }
        }
    }
}
3 respostas
solução!

Oi, Richard, tudo bem?

Ficou bem legal, parabéns pelos estudos! Acho que o que pode melhorar é um pouco da organização dos scripts, exemplo:

void PlayerCorrendo()
    {
        if(cansado == false)
        {
            if (Input.GetButtonDown("Fire3"))
            {
                Player.GetComponent<PlayerInput>().StatusPlayer.velocidade *= VelocidadeCorrendo;
                correndo = true;
            }
            else if (Input.GetButtonUp("Fire3"))
            {
                Player.GetComponent<PlayerInput>().StatusPlayer.velocidade /= VelocidadeCorrendo;
                correndo = false;
            }
        }
    }

Poderia abusar de um early return (guard clause) pra evitar esse if, algo como:

void PlayerCorrendo()
    {
        if(cansado) return; // se eu to cansado eu nem passo pras linhas de baixo

            if (Input.GetButtonDown("Fire3"))
            {
                Player.GetComponent<PlayerInput>().StatusPlayer.velocidade *= VelocidadeCorrendo;
                correndo = true;
            }
            else if (Input.GetButtonUp("Fire3"))
            {
                Player.GetComponent<PlayerInput>().StatusPlayer.velocidade /= VelocidadeCorrendo;
                correndo = false;
            }
    }

Mas esse tipo de coisa não vem muito de estudar Unity mas sim de estudar C#

Vlw, parece bem dahora :D terminando o projeto de zumbis, passo a estudar c# em paralelo.

Boa, achei legal que você evoluiu bem a lógica, isso é mais importante que escrever um código bonito.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software