Unity Beginner Tutorial: Moving, Rotating, and Scaling Objects

Click-to-Shoot & Destroy

Estimated reading: 4 minutes 19 views Contributors
  1. Create a Player that shoots projectiles.
  2. Have Targets that spawn automatically.
  3. When a projectile hits a target, the target is destroyed.
  4. Demonstrate OOP design: base class for game objects, inheritance for projectiles and targets.

Step-by-Step Unity Setup

1. Create a New Unity 3D Project

  • Open Unity Hub → New Project3D (URP optional) → Name it OOP_Shooter.
  • Once loaded, delete the default “SampleScene” objects if needed, keep the Main Camera and Directional Light.

2. Create a Basic Scene

  • Create an Empty GameObject called GameManager.
  • Create a Cube called Player → position at (0, 0, 0).
  • Create a Sphere prefab for projectiles → name it Projectile.
  • Create another Cube prefab for targets → name it Target.

Make sure you turn Target and Projectile into prefabs by dragging them into a Prefabs folder in the Project window.

Assets/
├── Scripts/
│ ├── BaseObject.cs
│ ├── PlayerController.cs
│ ├── Projectile.cs
│ ├── Target.cs
│ └── TargetSpawner.cs
├── Prefabs/
│ ├── Player.prefab
│ ├── Projectile.prefab
│ └── Target.prefab
└── Scenes/
└── MainScene.unity


Step 3: Scripts Overview (C#)

We’ll make 4 scripts:

ScriptPurpose
BaseObject.csBase class for shared logic
Projectile.csInherits from BaseObject
Target.csInherits from BaseObject
PlayerController.csHandles shooting and player input

BaseObject.cs

Demonstrates Encapsulation and Inheritance.

using UnityEngine;

public class BaseObject : MonoBehaviour
{
    [SerializeField] protected float health = 1f; // Encapsulation

    public virtual void TakeDamage(float damage)
    {
        health -= damage;
        if (health <= 0)
        {
            Destroy(gameObject);
        }
    }
}

Projectile.cs

Inherits from BaseObject, uses Polymorphism with OnCollisionEnter.

using UnityEngine;

public class Projectile : BaseObject
{
    [SerializeField] private float speed = 10f;
    [SerializeField] private float damage = 1f;

    void Update()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }

    private void OnCollisionEnter(Collision collision)
    {
        BaseObject target = collision.gameObject.GetComponent<BaseObject>();
        if (target != null && target != this)
        {
            target.TakeDamage(damage);
        }

        Destroy(gameObject);
    }
}

Target.cs

Inherits from BaseObject.
You could extend it to move or respawn.

using UnityEngine;

public class Target : BaseObject
{
    void Start()
    {
        // Give each target a random color to make it more fun
        GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value);
    }
}

PlayerController.cs

Shoots projectiles on click.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField] private GameObject projectilePrefab;
    [SerializeField] private Transform shootPoint;
    [SerializeField] private float shootForce = 500f;

    void Update()
    {
        if (Input.GetMouseButtonDown(0)) // Left click
        {
            Shoot();
        }
    }

    private void Shoot()
    {
        GameObject projectile = Instantiate(projectilePrefab, shootPoint.position, shootPoint.rotation);
        Rigidbody rb = projectile.GetComponent<Rigidbody>();
        rb.AddForce(shootPoint.forward * shootForce);
    }
}

Step 4: Hook It Up in the Editor

  1. Add Rigidbodies & Colliders:
    • Add a Rigidbody to Projectile and Target.
    • Ensure both have colliders (SphereCollider, BoxCollider).
    • Check Is Trigger = false.
  2. Player Setup:
    • Add the PlayerController script to the Player object.
    • Create an Empty Child under Player → name it ShootPoint → move it to the front of the player cube (e.g., (0, 0, 1)).
    • Drag the Projectile prefab into the Projectile Prefab field in the inspector.
  3. Target Setup:
    • Create a few Target prefabs in random positions around (x: ±5, y: 0, z: 10).

Optional Step: Add Target Spawner

To demonstrate composition and encapsulation, add this:

using UnityEngine;

public class TargetSpawner : MonoBehaviour
{
    [SerializeField] private GameObject targetPrefab;
    [SerializeField] private int targetCount = 5;
    [SerializeField] private Vector3 spawnArea = new Vector3(10, 0, 10);

    void Start()
    {
        for (int i = 0; i < targetCount; i++)
        {
            Vector3 pos = new Vector3(
                Random.Range(-spawnArea.x, spawnArea.x),
                spawnArea.y,
                Random.Range(5, spawnArea.z + 5)
            );
            Instantiate(targetPrefab, pos, Quaternion.identity);
        }
    }
}

Attach TargetSpawner to the GameManager object, assign the Target prefab.


Key OOP Concepts Demonstrated

ConceptWhere
EncapsulationBaseObject.health with protected access
InheritanceProjectile and Target inherit from BaseObject
PolymorphismTakeDamage() can behave differently in child classes
CompositionPlayerController uses composition (contains shoot logic, uses projectile prefab)

Run It!

Press Play → Left-click → your player shoots projectiles that destroy cubes when hit.
This project can easily be extended with:

  • Sound effects
  • UI score
  • Enemy movement
  • Reload timers

3. Build the Scene

  1. Create a new scene called MainScene and save it in Assets/Scenes/.
  2. Add:
    • Directional Light
    • Main Camera
    • GameManager (Empty Object) → attach TargetSpawner.
    • Player (Cube) → attach PlayerController.
      • Create a child object ShootPoint → position (0, 0, 1).
  3. Create Prefabs:
    • Projectile: Sphere with Rigidbody, Collider, and Projectile script.
    • Target: Cube with Rigidbody, Collider, and Target script.
    • Drag both into Assets/Prefabs/ as prefabs.
  4. Link in the inspector:
    • On PlayerController: assign Projectile Prefab to your Projectile.
    • On TargetSpawner: assign Target Prefab to your Target.

4. Test It

Click Play → left-click → see projectiles fire and destroy colored cubes 🎯
You can easily extend this with:

  • UI score counter
  • Target respawn delay
  • Player rotation with mouse

5. Export as a Unity Package

Once it works:

  1. In Unity, open Assets > Export Package…
  2. Check these folders:
    • Scripts
    • Prefabs
    • Scenes
  3. Click Export, name it: OOP_Shooter_QuickStart.unitypackage
  4. Done — you can now share or import it into any Unity project!
Share this Doc

Click-to-Shoot & Destroy

Or copy link

CONTENTS

Chat Icon Close Icon