Space Shooter Tutorial in Unity - Game Setup

by CGAfrica in Tutorials,

on December 21st, 2017

This post is part of series called Space Shooter tutorial in Unity

Creating a new project, importing assets, setting the build target and other project related details. Setting up the Player GameObject using mesh models, colliders and other built-in components. Setting up the main camera and lighting for the scene. Add a background image behind the game area. Creating shots by assembling artwork, physics components and custom C# code. Writing the code and setting up the scene to shoot shots.

Topics in this series


Setting up the project



 


The player GameObject



Camera and lighting


 


Adding a background



 


Moving the player



 


PlayerController 















C# JS BOO

using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;}
public class PlayerController : MonoBehaviour
{ public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()

{ float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
( Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax) );
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}}


class Boundary
{ var xMin : float;
var xMax : float;
var zMin : float;
var zMax : float;
}
var speed : float;
var tilt : float;
var boundary : Boundary;
function FixedUpdate () {
var moveHorizontal : float= Input.GetAxis ("Horizontal");
var moveVertical : float= Input.GetAxis ("Vertical");
var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;

rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);


import UnityEngine
import System.Collections
[System.Serializable]

public class Boundary:
public xMin as single
public xMax as single
public zMin as single
public zMax as single

public class PlayerController(MonoBehaviour):
public speed as single
public tilt as single
public boundary as Boundary


private def FixedUpdate():
moveHorizontal as single = Input.GetAxis('Horizontal')
moveVertical as single = Input.GetAxis('Vertical')
movement = Vector3(moveHorizontal, 0.0F, moveVertical)

rigidbody.velocity = (movement * speed)

rigidbody.position = Vector3(Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0F, Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax))

rigidbody.rotation = Quaternion.Euler(0.0F, 0.0F, (rigidbody.velocity.x * (-tilt)))







 


 Creating shots


Creating shots by assembling artwork, physics components and custom C# code.


 


 
















C#



JS



BOO



using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{ public float speed;
void Start ()

{rigidbody.velocity = transform.forward * speed;}
}


var speed : float;
function Start () : void {
rigidbody.velocity = transform.forward * speed;
}








import UnityEngine
import System.Collections
public class Mover(MonoBehaviour):

public speed as single
private def Start():
rigidbody.velocity = (transform.forward * speed)


 Shooting shots


Writing the code and setting up the scene to shoot shots.
















C# JS BOO

using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;

public GameObject shot;
public Transform shotSpawn;
public float fireRate;

private float nextFire;

void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}

void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;

rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}


class Boundary
{
var xMin : float;
var xMax : float;
var zMin : float;
var zMax : float;
}

var speed : float;
var tilt : float;
var boundary : Boundary;

var shot : GameObject;
var shotSpawn : Transform;
var fireRate : float;

private var nextFire : float;

function Update () {
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}

function FixedUpdate () {
var moveHorizontal : float= Input.GetAxis ("Horizontal");
var moveVertical : float= Input.GetAxis ("Vertical");

var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;

rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);

rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}










import UnityEngine
import System.Collections


[System.Serializable]
public class Boundary:
public xMin as single
public xMax as single
public zMin as single
public zMax as single


public class PlayerController(MonoBehaviour):
public speed as single
public tilt as single
public boundary as Boundary
public shot as GameObject
public shotSpawn as Transform
public fireRate as single
private nextFire as single

private def Update():
if Input.GetButton('Fire1') and (Time.time > nextFire):
nextFire = (Time.time + fireRate)
Instantiate(shot, shotSpawn.position, shotSpawn.rotation)

private def FixedUpdate():
moveHorizontal as single = Input.GetAxis('Horizontal')
moveVertical as single = Input.GetAxis('Vertical')

movement = Vector3(moveHorizontal, 0.0F, moveVertical)
rigidbody.velocity = (movement * speed)
rigidbody.position = Vector3(Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0F, Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax))
rigidbody.rotation = Quaternion.Euler(0.0F, 0.0F, (rigidbody.velocity.x * (-tilt)))
























Previous tutorial in series Next tutorial in series

cgafrica cgafrica cgafrica cgafrica

Please login to submit your comment