UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"Create code for a 3d fps shooter game where you fight zombies"
Creating a full 3D FPS shooter game with zombies would be too extensive to provide in a single response. However, I can provide you with a basic framework to get started. You can then build upon this foundation by adding more features, such as different weapons, enemy types, levels, and so on. 1. First, set up the player character with a first-person camera and basic movement controls: ```csharp using UnityEngine; public class PlayerController : MonoBehaviour { public float speed = 5.0f; public float mouseSensitivity = 2.0f; private float verticalRotation = 0; private CharacterController characterController; void Start() { characterController = GetComponent<CharacterController>(); Cursor.lockState = CursorLockMode.Locked; } void Update() { // Mouse look float horizontalRotation = Input.GetAxis("Mouse X") * mouseSensitivity; transform.Rotate(0, horizontalRotation, 0); verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity; verticalRotation = Mathf.Clamp(verticalRotation, -90, 90); Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0); // Movement float forwardSpeed = Input.GetAxis("Vertical") * speed; float sideSpeed = Input.GetAxis("Horizontal") * speed; Vector3 velocity = new Vector3(sideSpeed, 0, forwardSpeed); velocity = transform.rotation * velocity; characterController.SimpleMove(velocity); } } ``` 2. Create a simple shooting mechanic: ```csharp using UnityEngine; public class GunController : MonoBehaviour { public GameObject bulletPrefab; public Transform bulletSpawn; public float fireRate = 0.5f; private float nextFire = 0.0f; void Update() { if (Input.GetButton("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; Shoot(); } } void Shoot() { GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation); bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 20; Destroy(bullet, 2.0f); } } ``` 3. Create a basic zombie AI: ```csharp using UnityEngine; using UnityEngine.AI; public class ZombieAI : MonoBehaviour { public Transform player; public float chaseDistance = 10.0f; private NavMeshAgent navMeshAgent; void Start() { navMeshAgent = GetComponent<NavMeshAgent>(); } void Update() { float distanceToPlayer = Vector3.Distance(transform.position, player.position); if (distanceToPlayer < chaseDistance) { navMeshAgent.SetDestination(player.position); } } } ``` 4. Add health and damage mechanics: ```csharp using UnityEngine; public class Health : MonoBehaviour { public float maxHealth = 100; public float currentHealth; void Start() { currentHealth = maxHealth; } public void TakeDamage(float damage) { currentHealth -= damage; if (currentHealth <= 0) { Die(); } } void Die() { if (gameObject.CompareTag("Player")) { // Implement player death logic } else if (gameObject.CompareTag("Zombie")) { Destroy(gameObject); } } } ``` 5. Add a script to handle bullet collisions and apply damage: ```csharp using UnityEngine; public class Bullet : MonoBehaviour { public float damage = 10f; void OnCollisionEnter(Collision collision) { Health health = collision.collider.GetComponent<Health>(); if (health != null) { health.TakeDamage(damage); } Destroy(gameObject); } } ``` This is a basic framework for a 3D FPS shooter game with zombies. You can build upon this by adding more features, such as different weapons, enemy types, levels, and so on.