Code Stuff

1. Import Character Models

  1. Download or import character models:
  • Use Unity’s Asset Store to find free character models.
  • Or use models from sites like Mixamo or Sketchfab.
  1. Drag the models into the Hierarchy.
  2. Position the characters in the scene (like before):
  • Player1: Left side (e.g., X = -3).
  • Player2: Right side (e.g., X = 3).

2. Add Colliders and Rigidbodies

  1. Select each character model (e.g., Player1):
  • Add a Capsule Collider to match the character’s body shape.
  • Add a Rigidbody:
    • Set Body Type to Kinematic (to avoid physics issues).
  1. Add Punch Colliders:
  • Create an Empty GameObject under each character (name it PunchHitbox).
  • Position it near the character’s hands.
  • Add a Box Collider to the PunchHitbox:
    • Check Is Trigger.

3. Attach Player Movement and Punch Scripts

Use the same PlayerController3D script I gave earlier to move the characters and trigger punches.

  1. Attach the script to your character models.
  2. Adjust the script:
  • Set movement controls:
    • Player1: W (forward), S (backward), Space (punch).
    • Player2: Arrow keys and Right Shift.
  • Drag the PunchHitbox into the punchHitbox field.

using UnityEngine;

public class PlayerController3D : MonoBehaviour
{
public KeyCode forwardKey; // Move forward
public KeyCode backwardKey; // Move backward
public KeyCode punchKey; // Punch key
public float moveSpeed = 5f; // Movement speed
public GameObject punchHitbox; // Punch hitbox

void Update()
{
    // Move forward and backward
    if (Input.GetKey(forwardKey))
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    if (Input.GetKey(backwardKey))
        transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);

    // Punch
    if (Input.GetKeyDown(punchKey))
    {
        StartCoroutine(Punch());
    }
}

// Show hitbox briefly
System.Collections.IEnumerator Punch()
{
    punchHitbox.SetActive(true);
    yield return new WaitForSeconds(0.2f);
    punchHitbox.SetActive(false);
}

}

4. Trigger Punch Animations

If your character models have animations (from Mixamo or elsewhere):

  1. Add an Animator Controller:
  • Select Player1 > Add Component > Animator.
  • Drag an Animator Controller into the Controller field.
  1. Create Punch Animation:
  • Import the punch animation with the character model (Mixamo has great ones).
  • In the Animator window, set up a Trigger parameter called Punch.
  • Link it to the punch animation.
  1. Modify the PlayerController3D script to trigger the animation:

void Update()
{
if (Input.GetKey(forwardKey))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if (Input.GetKey(backwardKey))
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);

if (Input.GetKeyDown(punchKey))
{
    GetComponent<Animator>().SetTrigger("Punch");
    StartCoroutine(Punch());
}

}

5. Detect Punch Collisions

Use the same PunchDetection3D script to detect when a punch lands on the opponent.

  • Ensure the opponent character models are tagged (Player1 or Player2).
  • Attach PunchDetection3D to the PunchHitbox.

using UnityEngine;

public class PunchDetection3D : MonoBehaviour
{
public BoxingScore3D scoreManager; // Link to scoring system
public bool isPlayer1Punch; // Check which player

void OnTriggerEnter(Collider other)
{
    if (isPlayer1Punch && other.CompareTag("Player2"))
    {
        scoreManager.Player1Scores();
    }
    else if (!isPlayer1Punch && other.CompareTag("Player1"))
    {
        scoreManager.Player2Scores();
    }
}

}

6. Scoring System

Use the same BoxingScore3D script as before. Link the UI Text to display scores for Player1 and Player2.

using UnityEngine;
using UnityEngine.UI;

public class BoxingScore3D : MonoBehaviour
{
public Text player1ScoreText;
public Text player2ScoreText;

private int player1Score = 0;
private int player2Score = 0;

public void Player1Scores()
{
    player1Score++;
    player1ScoreText.text = "Player 1: " + player1Score;
}

public void Player2Scores()
{
    player2Score++;
    player2ScoreText.text = "Player 2: " + player2Score;
}

}