Initial commit - Unity game project
This commit is contained in:
106
Assets/Scripts/FirstPersonController.cs
Normal file
106
Assets/Scripts/FirstPersonController.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class FirstPersonController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement Settings")]
|
||||
public float walkSpeed = 8f;
|
||||
public float runSpeed = 14f;
|
||||
public float jumpHeight = 2.5f;
|
||||
public float gravity = -20f;
|
||||
|
||||
[Header("Mouse Look Settings")]
|
||||
public float mouseSensitivity = 3f;
|
||||
public float maxLookAngle = 90f;
|
||||
|
||||
[Header("References")]
|
||||
public Camera playerCamera;
|
||||
|
||||
// Private variables
|
||||
private CharacterController controller;
|
||||
private Vector3 velocity;
|
||||
private bool isGrounded;
|
||||
private float xRotation = 0f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Get the CharacterController component
|
||||
controller = GetComponent<CharacterController>();
|
||||
|
||||
// If no camera is assigned, try to find one
|
||||
if (playerCamera == null)
|
||||
{
|
||||
playerCamera = GetComponentInChildren<Camera>();
|
||||
}
|
||||
|
||||
// Lock and hide the cursor
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Check if player is on the ground
|
||||
isGrounded = controller.isGrounded;
|
||||
|
||||
// Reset vertical velocity when grounded
|
||||
if (isGrounded && velocity.y < 0)
|
||||
{
|
||||
velocity.y = -2f;
|
||||
}
|
||||
|
||||
// Get movement input
|
||||
float moveX = Input.GetAxis("Horizontal"); // A/D or Left/Right arrows
|
||||
float moveZ = Input.GetAxis("Vertical"); // W/S or Up/Down arrows
|
||||
|
||||
// Calculate movement direction
|
||||
Vector3 move = transform.right * moveX + transform.forward * moveZ;
|
||||
|
||||
// Determine current speed (run if Shift is held)
|
||||
float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
|
||||
|
||||
// Move the character
|
||||
controller.Move(move * currentSpeed * Time.deltaTime);
|
||||
|
||||
// Jumping
|
||||
if (Input.GetButtonDown("Jump") && isGrounded)
|
||||
{
|
||||
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
}
|
||||
|
||||
// Apply gravity
|
||||
velocity.y += gravity * Time.deltaTime;
|
||||
controller.Move(velocity * Time.deltaTime);
|
||||
|
||||
// Mouse look
|
||||
HandleMouseLook();
|
||||
|
||||
// Press Escape to unlock cursor
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
|
||||
// Click to lock cursor again
|
||||
if (Input.GetMouseButtonDown(0) && Cursor.lockState == CursorLockMode.None)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMouseLook()
|
||||
{
|
||||
// Get mouse input
|
||||
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
|
||||
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
|
||||
|
||||
// Rotate camera up/down (pitch)
|
||||
xRotation -= mouseY;
|
||||
xRotation = Mathf.Clamp(xRotation, -maxLookAngle, maxLookAngle);
|
||||
playerCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
||||
|
||||
// Rotate player left/right (yaw)
|
||||
transform.Rotate(Vector3.up * mouseX);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/FirstPersonController.cs.meta
Normal file
11
Assets/Scripts/FirstPersonController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b87887b68361bb4dbb1327a9bb7aa82
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
44
Assets/Scripts/SimpleCrosshair.cs
Normal file
44
Assets/Scripts/SimpleCrosshair.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SimpleCrosshair : MonoBehaviour
|
||||
{
|
||||
[Header("Crosshair Settings")]
|
||||
public Color crosshairColor = Color.white;
|
||||
public float crosshairSize = 10f;
|
||||
public float crosshairThickness = 2f;
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
// Calculate center of screen
|
||||
float centerX = Screen.width / 2f;
|
||||
float centerY = Screen.height / 2f;
|
||||
|
||||
// Set the crosshair color
|
||||
GUI.color = crosshairColor;
|
||||
|
||||
// Draw horizontal line
|
||||
GUI.DrawTexture(
|
||||
new Rect(
|
||||
centerX - crosshairSize / 2f,
|
||||
centerY - crosshairThickness / 2f,
|
||||
crosshairSize,
|
||||
crosshairThickness
|
||||
),
|
||||
Texture2D.whiteTexture
|
||||
);
|
||||
|
||||
// Draw vertical line
|
||||
GUI.DrawTexture(
|
||||
new Rect(
|
||||
centerX - crosshairThickness / 2f,
|
||||
centerY - crosshairSize / 2f,
|
||||
crosshairThickness,
|
||||
crosshairSize
|
||||
),
|
||||
Texture2D.whiteTexture
|
||||
);
|
||||
|
||||
// Reset GUI color
|
||||
GUI.color = Color.white;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/SimpleCrosshair.cs.meta
Normal file
11
Assets/Scripts/SimpleCrosshair.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a8ef389adf1c8b498e2e5d270f4ed94
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
127
Assets/Scripts/SimpleGun.cs
Normal file
127
Assets/Scripts/SimpleGun.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SimpleGun : MonoBehaviour
|
||||
{
|
||||
[Header("Gun Settings")]
|
||||
public float damage = 25f;
|
||||
public float range = 100f;
|
||||
public float fireRate = 0.5f;
|
||||
public int maxAmmo = 30;
|
||||
public int currentAmmo;
|
||||
|
||||
[Header("References")]
|
||||
public Camera fpsCam;
|
||||
public ParticleSystem muzzleFlash;
|
||||
|
||||
private float nextTimeToFire = 0f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
currentAmmo = maxAmmo;
|
||||
|
||||
// Find camera if not assigned
|
||||
if (fpsCam == null)
|
||||
{
|
||||
fpsCam = Camera.main;
|
||||
}
|
||||
|
||||
// Create simple gun visuals
|
||||
CreateSimpleGunModel();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Shoot on left mouse button
|
||||
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
|
||||
{
|
||||
nextTimeToFire = Time.time + 1f / fireRate;
|
||||
Shoot();
|
||||
}
|
||||
|
||||
// Reload on R key
|
||||
if (Input.GetKeyDown(KeyCode.R))
|
||||
{
|
||||
Reload();
|
||||
}
|
||||
}
|
||||
|
||||
void CreateSimpleGunModel()
|
||||
{
|
||||
// Main body
|
||||
GameObject body = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
body.transform.SetParent(transform);
|
||||
body.transform.localPosition = new Vector3(0, -0.1f, 0.3f);
|
||||
body.transform.localScale = new Vector3(0.1f, 0.15f, 0.4f);
|
||||
body.transform.localRotation = Quaternion.identity;
|
||||
Destroy(body.GetComponent<Collider>()); // Remove collider
|
||||
|
||||
// Set color
|
||||
Renderer bodyRenderer = body.GetComponent<Renderer>();
|
||||
bodyRenderer.material.color = new Color(0.2f, 0.2f, 0.2f); // Dark gray
|
||||
|
||||
// Barrel
|
||||
GameObject barrel = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
|
||||
barrel.transform.SetParent(transform);
|
||||
barrel.transform.localPosition = new Vector3(0, -0.05f, 0.6f);
|
||||
barrel.transform.localScale = new Vector3(0.03f, 0.15f, 0.03f);
|
||||
barrel.transform.localRotation = Quaternion.Euler(90, 0, 0);
|
||||
Destroy(barrel.GetComponent<Collider>());
|
||||
|
||||
Renderer barrelRenderer = barrel.GetComponent<Renderer>();
|
||||
barrelRenderer.material.color = new Color(0.1f, 0.1f, 0.1f); // Even darker
|
||||
|
||||
// Handle
|
||||
GameObject handle = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
handle.transform.SetParent(transform);
|
||||
handle.transform.localPosition = new Vector3(0, -0.25f, 0.15f);
|
||||
handle.transform.localScale = new Vector3(0.08f, 0.2f, 0.1f);
|
||||
handle.transform.localRotation = Quaternion.Euler(15, 0, 0);
|
||||
Destroy(handle.GetComponent<Collider>());
|
||||
|
||||
Renderer handleRenderer = handle.GetComponent<Renderer>();
|
||||
handleRenderer.material.color = new Color(0.3f, 0.2f, 0.1f); // Brown
|
||||
}
|
||||
|
||||
void Shoot()
|
||||
{
|
||||
if (currentAmmo <= 0)
|
||||
{
|
||||
Debug.Log("Out of ammo! Press R to reload.");
|
||||
return;
|
||||
}
|
||||
|
||||
currentAmmo--;
|
||||
Debug.Log($"Shot fired! Ammo: {currentAmmo}/{maxAmmo}");
|
||||
|
||||
// Play muzzle flash if assigned
|
||||
if (muzzleFlash != null)
|
||||
{
|
||||
muzzleFlash.Play();
|
||||
}
|
||||
|
||||
// Raycast from camera center
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
|
||||
{
|
||||
Debug.Log($"Hit: {hit.transform.name}");
|
||||
|
||||
// You can add hit detection for enemies here later
|
||||
// Example: hit.transform.GetComponent<Enemy>()?.TakeDamage(damage);
|
||||
}
|
||||
}
|
||||
|
||||
void Reload()
|
||||
{
|
||||
Debug.Log("Reloading...");
|
||||
currentAmmo = maxAmmo;
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
// Simple ammo counter in bottom-right
|
||||
GUI.color = Color.white;
|
||||
GUI.Label(new Rect(Screen.width - 120, Screen.height - 40, 100, 30),
|
||||
$"Ammo: {currentAmmo}/{maxAmmo}",
|
||||
new GUIStyle() { fontSize = 20, normal = new GUIStyleState() { textColor = Color.white } });
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/SimpleGun.cs.meta
Normal file
11
Assets/Scripts/SimpleGun.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e348b4e172ba23d45960c0071cc09b1f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user