Skip to content

Latest commit

 

History

History
229 lines (163 loc) · 7.09 KB

UnityAI.md

File metadata and controls

229 lines (163 loc) · 7.09 KB

// 12/10/2025 AI-Tag // This was created with the help of Assistant, a Unity Artificial Intelligence product.

Creating a 2D game similar to Angry Birds in Unity involves several steps, including setting up the Unity project, creating assets, implementing game mechanics, and writing scripts. Below is a detailed guide to help you build the game:


Step 1: Set Up the Unity Project

  1. Open Unity and create a new 2D project.
  2. Set up the scene:
    • Add a Main Camera and set its background color.
    • Add a Canvas for UI elements (score display, restart button).
    • Add a Ground object (a simple 2D rectangle or sprite) to serve as the base.

Step 2: Create Assets

  1. Projectile (Bird):

    • Create a simple circle sprite or use a placeholder image.
    • Add a Rigidbody2D and CircleCollider2D to the projectile.
  2. Structures (Blocks):

    • Create rectangular sprites for wood, stone, and glass blocks.
    • Add Rigidbody2D and BoxCollider2D to each block.
    • Adjust the Mass and Material properties in the Rigidbody2D to differentiate materials.
  3. Targets (Pigs):

    • Use a simple circle sprite or placeholder image.
    • Add Rigidbody2D and CircleCollider2D.
  4. Slingshot:

    • Create a visual representation of the slingshot using simple shapes or sprites.
  5. Background:

    • Add a static background image or color.

Step 3: Implement Game Mechanics

Script 1: Slingshot Mechanism

This script handles the drag-and-release functionality for launching the projectile.

using UnityEngine;

public class Slingshot : MonoBehaviour
{
    public Transform slingOrigin; // The origin point of the slingshot
    public LineRenderer slingLine; // LineRenderer for visualizing the slingshot
    public Rigidbody2D projectile; // The projectile to launch
    public float launchForce = 10f;

    private Vector2 dragStartPos;
    private bool isDragging = false;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            dragStartPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            isDragging = true;
        }

        if (Input.GetMouseButton(0) && isDragging)
        {
            Vector2 dragCurrentPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 dragDirection = dragStartPos - dragCurrentPos;

            slingLine.SetPosition(0, slingOrigin.position);
            slingLine.SetPosition(1, dragCurrentPos);
        }

        if (Input.GetMouseButtonUp(0) && isDragging)
        {
            Vector2 dragEndPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 launchDirection = dragStartPos - dragEndPos;

            projectile.isKinematic = false;
            projectile.AddForce(launchDirection * launchForce, ForceMode2D.Impulse);

            slingLine.enabled = false;
            isDragging = false;
        }
    }
}

Script 2: Block Behavior

This script handles the destruction of blocks when hit with sufficient force.

using UnityEngine;

public class Block : MonoBehaviour
{
    public float health = 100f;

    void OnCollisionEnter2D(Collision2D collision)
    {
        float impactForce = collision.relativeVelocity.magnitude;

        if (impactForce > 10f) // Adjust threshold as needed
        {
            health -= impactForce * 10f;
        }

        if (health <= 0)
        {
            Destroy(gameObject);
        }
    }
}

Script 3: Target Behavior

This script handles the elimination of targets (pigs).

using UnityEngine;

public class Target : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.relativeVelocity.magnitude > 5f) // Adjust threshold as needed
        {
            Destroy(gameObject);
        }
    }
}

Script 4: Scoring System

This script tracks and displays the player's score.

using UnityEngine;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour
{
    public Text scoreText;
    private int score = 0;

    public void AddScore(int points)
    {
        score += points;
        scoreText.text = "Score: " + score;
    }
}

Script 5: Level Manager

This script handles restarting the level.

using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour
{
    public void RestartLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}

Step 4: Add UI Elements

  1. Add a Text element to the Canvas for displaying the score.
  2. Add a Button element for restarting the level and link it to the RestartLevel method in the LevelManager.

Step 5: Configure Physics

  1. Adjust the Gravity Scale in the Rigidbody2D components to simulate realistic gravity.
  2. Use Physics2D Materials to define friction and bounciness for different materials (wood, stone, glass).

Step 6: Test and Iterate

  1. Play the game and test the mechanics.
  2. Adjust parameters (e.g., launch force, block health) for better gameplay.
  3. Add more levels by creating new scenes with different block and target placements.

This setup provides a basic framework for your Angry Birds-inspired game. You can expand it by adding more levels, improving graphics, or introducing new mechanics. Let me know if you'd like help with any specific part!

image image image

UnityAI;

image image image image image image image image image