como criar um jogo de luta na UNITY C#/
CSeguros e produos financeiros com atendimento via WhatsApp
1. CRIAR O PLAYER
No Unity:
-
Crie um
GameObject > 2D Object > Sprite (Square)e nomeie dePlayer1. -
Adicione um BoxCollider2D e um Rigidbody2D.
-
Faça o mesmo para o
Player2. -
Crie uma tag "Player1" e "Player2".
2. SCRIPT DE MOVIMENTAÇÃO E ATAQUE
Crie um script chamado Fighter.cs e cole o seguinte código:
csharpusing UnityEngine;
public class Fighter : MonoBehaviour
{
public float speed = 5f;
public float jumpForce = 7f;
public int maxHealth = 100;
public Transform attackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
public int attackDamage = 10;
public string horizontalAxis = "Horizontal";
public KeyCode jumpKey = KeyCode.W;
public KeyCode attackKey = KeyCode.Space;
private Rigidbody2D rb;
private int currentHealth;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
currentHealth = maxHealth;
}
private void Update()
{
// Movimento
float move = Input.GetAxis(horizontalAxis);
rb.velocity = new Vector2(move * speed, rb.velocity.y);
// Pulo
if (Input.GetKeyDown(jumpKey) && Mathf.Abs(rb.velocity.y) < 0.01f)
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
// Ataque
if (Input.GetKeyDown(attackKey))
{
Attack();
}
}
void Attack()
{
// Detectar inimigos na área de ataque
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Fighter>().TakeDamage(attackDamage);
}
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
Debug.Log(gameObject.name + " levou dano! Vida: " + currentHealth);
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log(gameObject.name + " morreu!");
gameObject.SetActive(false);
}
private void OnDrawGizmosSelected()
{
if (attackPoint == null) return;
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
3. CONFIGURAÇÃO DO PLAYER NO UNITY
Para cada player:
-
Adicione esse script
Fighter.cs. -
Crie um Empty GameObject como filho chamado
AttackPoint(posicione onde for o "soco"). -
No Inspector, arraste o
AttackPointpara o campo do script. -
Configure:
-
Player 1:
-
horizontalAxis:"Horizontal"(padrão: A/D) -
jumpKey:W -
attackKey:Space
-
-
Player 2:
-
horizontalAxis:"Horizontal2"(você precisará criar no InputManager) -
jumpKey: por exemploUpArrow -
attackKey:RightControlou outro
-
-
4. CONFIGURAR ENTRADAS (InputManager)
Para adicionar eixos personalizados:
-
Vá em
Edit > Project Settings > Input Manager -
Duplique o eixo "Horizontal"
-
Renomeie para "Horizontal2"
-
Configure para usar teclas diferentes (Ex: LeftArrow, RightArrow)
5. LAYER E TAGS
-
Crie uma Layer chamada
Player1ePlayer2 -
Atribua os jogadores a essas layers
-
No campo
enemyLayersdo script, atribua o layer do inimigo correspondente
6. UI DE VIDA (Opcional)
Se quiser, você pode adicionar uma barra de vida com Slider UI para mostrar a saúde dos jogadores visualmente.
- Gerar link
- X
- Outros aplicativos
Comentários
Postar um comentário