If you have always wanted to create your own game but felt intimidated by game development, this project is a great place to begin. In this tutorial we create a simple side-scrolling shooter in Unity using freely available assets and a few straightforward C# scripts.
By the end of this tutorial, you will have:
• A scrolling background
• A controllable aircraft
• Bullet firing mechanics
• Smooth acceleration and movement
• An understanding of Unity Prefabs and scripting basics
The best part is that every feature can be extended to create your own unique game.
- 1 Plane with 3 animations: fly, shoot, & dead
- Simple background
- Fully editable vector source files in SVG and AI file formats.
- Separate PNG sequence files for quick integration in your game projects
using System.Collections.Generic;
using UnityEngine;
public class Scroller : MonoBehaviour {
//Public field shows up in the inspector so it can be easily
//tweaked even while the game is //running
public float scrollSpeed;
//Private fields dont show up in the inspector
private float scrollValue;
private MeshRenderer renderer;
// Use this for initialization
void Start () {
renderer = GetComponent<MeshRenderer>();
}
// Update is called once per frame
void Update () {
// Update the scroll value .....Time.deltaTime is used to make sure the
// scrolling is independent of the fps the game is running on.
scrollValue += scrollSpeed*Time.deltaTime;
// UV values range from 0 - 1 so there is no point in exceeding the
// value > 1 , so we reset the value back to 0 , in order to loop it. if(scrollValue >= 1f){
scrollValue = 0f;
}
//Update the Main texture offset X value to make it scroll horizontally.
renderer.material.mainTextureOffset = new Vector2(scrollValue,0f);
}
}
Challenge yourself: Try changing the value of scrollSpeed and observe how the feel of the game changes. Small modifications like this are one of the best ways to learn Unity.
What happens?
using UnityEngine;
public class Player : MonoBehaviour
{
public float accel = 0f;
public float currentSpeed;
public float targetSpeed;
public float fireDelay = 0f;
private float timeElapsed = 0f;
public GameObject bulletPrefab;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//Detect Keyboard Input
if (Input.GetKey(KeyCode.S))
{
targetSpeed = -5f;
}
else if (Input.GetKey(KeyCode.W))
{
targetSpeed = 5f;
}
else
{
targetSpeed = 0f;
}
//Lerp the CurrentSpeed towards the targetSpeed
MoveToTargetSpeed();
if (Input.GetKey(KeyCode.Space))
{
/* ADD a delay between each time the player fires the bullet , so we dont spam the frame with multiple bullet objects , usually 0.1s dealy works goodbut it all depends on ur game design and difficulty.This variable can also be used as an upgrade option , where the players can change their ships firing speed.*/ timeElapsed += Time.deltaTime;
if (timeElapsed >= fireDelay)
{
Fire();
timeElapsed = 0f;
}
}
}
void MoveToTargetSpeed()
{
/* Increment or Decrement the currentSpeed value */
currentSpeed += accel * Time.deltaTime * Mathf.Sign(targetSpeed - currentSpeed);
/* This is where the bug happens but it is a nice feature to have so i kept this poor piece of logic */
if (Mathf.Abs(Mathf.Abs(targetSpeed) - Mathf.Abs(currentSpeed)) < 0.01f)
{
currentSpeed = targetSpeed;
}
/* Store the Next Position to update in temp var so that we can check if the player is reaching the bonds of the device's screen you dont want the player to go off the screen. Camera.main.WorldToScreenPoint is a helpful method to convert any Vector in world space to screen space. We need the player Coordinates in screen space so we can compare it to the device height and width which are pixel values */
Vector3 nextPos = new Vector3(transform.position.x, transform.position.y + currentSpeed * Time.deltaTime, transform.position.z);
if (Camera.main.WorldToScreenPoint(nextPos).y > Screen.height || Camera.main.WorldToScreenPoint(nextPos).y < 0f)
{
/* if players next position is crossing the device's screen bounds then stop the movement and return to Update , this skips the actual position update we r doing in the last line of this method*/ currentSpeed = 0f;
targetSpeed = 0f;
return; }
// if everything is alright then update the player position. transform.position = nextPos;
}
void Fire()
{// rotation should be the default prefab rotation.
Instantiate(bulletPrefab, transform.position, Quaternion.identity);
}
}
````The above player script is pretty straightforward, check for inputs, set targetSpeed accordingly and lerp the current speed towards the target speed to get a feeling of acceleration.
One of the most interesting things happened accidentally. A tiny imperfection in the movement logic caused the plane to drift slightly up and down. Instead of removing it immediately, I kept it because it gave the aircraft a subtle turbulence effect, making the movement feel more natural.
Sometimes, bugs become features!
````
using System;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed;
void Update()
{
transform.Translate(new Vector3(speed,0f,0f)*Time.deltaTime);
}
}
````
Final Thoughts
This project shows that game development is not about writing thousands of lines of code. With a few sprites, a scrolling background, prefabs and simple scripts, you can already create a playable game.
If you are a student learning Unity, I encourage you to modify this project:
Add enemies
Add explosions and sound effects
Implement a score system
Introduce multiple levels
Design your own aircraft
Every successful game begins with a small experiment. This side scroller could be yours.
No comments:
Post a Comment