Untitled
unknown
plain_text
10 months ago
3.6 kB
3
Indexable
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
private Vector2 movement;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// Input for movement
movement.x = Input.GetAxis("Horizontal");
movement.y = Input.GetAxis("Vertical");
}
void FixedUpdate()
{
// Apply movement
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
using UnityEngine;
public class SurvivalStats : MonoBehaviour
{
public float health = 100f;
public float hunger = 100f;
public float thirst = 100f;
public float hungerDecayRate = 0.5f;
public float thirstDecayRate = 1f;
void Update()
{
// Decrease hunger and thirst over time
hunger -= hungerDecayRate * Time.deltaTime;
thirst -= thirstDecayRate * Time.deltaTime;
// Reduce health if hunger or thirst reaches zero
if (hunger <= 0 || thirst <= 0)
{
health -= 5f * Time.deltaTime;
}
// Clamp values between 0 and 100
hunger = Mathf.Clamp(hunger, 0, 100);
thirst = Mathf.Clamp(thirst, 0, 100);
health = Mathf.Clamp(health, 0, 100);
// Check for death
if (health <= 0)
{
Debug.Log("Game Over!");
// Add game over logic here
}
}
public void Eat(float foodValue)
{
hunger += foodValue;
}
public void Drink(float waterValue)
{
thirst += waterValue;
}
}
using UnityEngine;
public class ResourceManager : MonoBehaviour
{
public int wood = 0;
public int stone = 0;
public int water = 0;
public void CollectResource(string resourceType)
{
switch (resourceType)
{
case "Wood":
wood += 1;
break;
case "Stone":
stone += 1;
break;
case "Water":
water += 1;
break;
default:
Debug.Log("Unknown resource type!");
break;
}
Debug.Log($"Collected: {resourceType}");
}
}
using UnityEngine;
public class GameManager : MonoBehaviour
{
public SurvivalStats playerStats;
public ResourceManager resourceManager;
void Update()
{
// Example interaction logic
if (Input.GetKeyDown(KeyCode.E)) // Simulate eating
{
if (resourceManager.wood > 0) // Imagine "wood" is food for now
{
playerStats.Eat(20);
resourceManager.wood--;
Debug.Log("You ate some food!");
}
}
if (Input.GetKeyDown(KeyCode.W)) // Simulate drinking
{
if (resourceManager.water > 0)
{
playerStats.Drink(20);
resourceManager.water--;
Debug.Log("You drank water!");
}
}
}
}
using UnityEngine;
public class Resource : MonoBehaviour
{
public string resourceType; // "Wood", "Stone", or "Water"
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
other.GetComponent<ResourceManager>().CollectResource(resourceType);
Destroy(gameObject); // Remove resource after collection
}
}
}
Editor is loading...
Leave a Comment