Place Player1
at (x = -3, y = 1, z = 0)
and Player2
at (x = 3, y = 1, z = 0)
on the boxing ring.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float rotationSpeed = 700f;
private string horizontalAxis;
private string verticalAxis;
void Start()
{
// Set controls for Player 1 and Player 2
if (gameObject.name == "Player1")
{
horizontalAxis = "Horizontal";
verticalAxis = "Vertical";
}
else if (gameObject.name == "Player2")
{
horizontalAxis = "Horizontal2";
verticalAxis = "Vertical2";
}
}
void Update()
{
float moveX = Input.GetAxis(horizontalAxis) * moveSpeed * Time.deltaTime;
float moveZ = Input.GetAxis(verticalAxis) * moveSpeed * Time.deltaTime;
// Move the player
transform.Translate(new Vector3(moveX, 0, moveZ), Space.World);
// Rotate player to face the movement direction
if (moveX != 0 || moveZ != 0)
{
Quaternion toRotation = Quaternion.LookRotation(new Vector3(moveX, 0, moveZ), Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
}
}
- Go to Edit > Project Settings > Input Manager.
- Duplicate the axes Horizontal and Vertical, renaming them to
Horizontal2
andVertical2
for Player 2. - Configure Player 2’s controls to
WASD
(or any other keys you’d like).
using UnityEngine;
public class PlayerPunch : MonoBehaviour
{
public float punchRange = 1.5f; // How far the punch can reach
public float punchCooldown = 0.5f; // Time between punches
private float nextPunchTime = 0f;
public string punchButton; // Which button to press for punching
void Update()
{
if (Input.GetButtonDown(punchButton) && Time.time >= nextPunchTime)
{
nextPunchTime = Time.time + punchCooldown;
Punch();
}
}
void Punch()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, punchRange))
{
if (hit.transform.CompareTag("Player"))
{
// Player is within range, handle punch hit
Debug.Log("Punch landed on: " + hit.transform.name);
// Here you can add logic for damage or knockback
}
}
}
}
- Attach the
PlayerPunch
script to bothPlayer1
andPlayer2
. - Set
punchButton
forPlayer1
to"Fire1"
(left mouse button or Ctrl) and forPlayer2
to"Fire2"
(right mouse button or Alt).
using UnityEngine;
public class Health : MonoBehaviour
{
public int health = 100;
public GameObject healthUI; // Optional: to update UI (health bar) for the player
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
void Die()
{
// Handle player death (e.g., game over, or reset the game)
Debug.Log(gameObject.name + " is out!");
}
}
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public Health player1Health;
public Health player2Health;
public Text winText; // UI Text to display the winner message
void Update()
{
// Check if Player 1 or Player 2's health has reached zero
if (player1Health.health <= 0)
{
winText.text = "Player 2 Wins!";
Time.timeScale = 0; // Pause the game
}
else if (player2Health.health <= 0)
{
winText.text = "Player 1 Wins!";
Time.timeScale = 0; // Pause the game
}
}
}