Untitled
unknown
plain_text
10 months ago
2.0 kB
3
Indexable
using UnityEngine;
using UnityEngine.UI;
public class WarriorMovement : MonoBehaviour
{
public float speed = 5f;
public float rotationSpeed = 700f;
public float jumpForce = 7f;
public float maxHealth = 100f;
public float currentHealth;
public float fireRate = 0.5f;
public Transform firePoint;
public GameObject bulletPrefab;
public Text healthText;
private Rigidbody rb;
private bool isGrounded;
private float nextFireTime;
void Start()
{
rb = GetComponent<Rigidbody>();
currentHealth = maxHealth;
UpdateHealthUI();
}
void Update()
{
// Move the player forward and backward
float move = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.Translate(0, 0, move);
// Rotate the player left and right
float rotate = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
transform.Rotate(0, rotate, 0);
// Jump
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
// Shooting
if (Input.GetButton("Fire1") && Time.time > nextFireTime)
{
nextFireTime = Time.time + fireRate;
Shoot();
}
}
void OnCollisionStay(Collision collision)
{
isGrounded = true;
}
void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
void Shoot()
{
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
public void TakeDamage(float amount)
{
currentHealth -= amount;
UpdateHealthUI();
if (currentHealth <= 0f)
{
Die();
}
}
void UpdateHealthUI()
{
healthText.text = "Health: " + currentHealth.ToString();
}
void Die()
{
// Handle player death (e.g., respawn, end game)
Debug.Log("Player Died!");
}
}
Editor is loading...
Leave a Comment