Friday 30 January 2015

Every game needs a timer and moving platforms Right?

Obviously the answer is no, but it seems like the best mind set to have when creating the mechanics for your game. No one wants to spend hours re-writing the same scripts over and over again and I found myself doing this whenever starting a new project. I have promised myself to try and make my life easier from now on by trying my best to make the code as re-usable as possible and have them stored for a rainy day. I thought I would share a couple scripts (JavaScript) that others might find useful and that follow my new train of thought.

Timer:

#pragma strict

public var scaleOfBar :float;
private var total :float;
private var percent :float;
public var duration :float;
public var horizontal :boolean = false;
public var vertical :boolean = false;
public var minus :boolean = false;
public var adding :boolean = false;


function Start () {

if (minus) // If ticked in unity the bar will decrease in scale.
total = scaleOfBar;
else if (adding) // This will increase.
total = 0;
}

function Update ()
{
percent = scaleOfBar / duration; // How much to Increase/Decrease the bar every second

if (total > 0 && minus)
total -= Time.deltaTime * percent; //Decrease until bar is empty
if (total < scaleOfBar && adding)
total += Time.deltaTime * percent; //Increases bar until it's full

// Statements below check what axis to effect
if (horizontal)
transform.localScale.x = total;
if (vertical)
transform.localScale.y = total;
}

Once attached to your object you just need to change the pivot point so the bar does what you want.
Fill in all the information in the Unity editor (duration is in seconds) by ticking the boxes that you want. Note: If you have a horizontal bar copy and paste the x Scale of the bar into scaleOfBar or the y Scale for vertical.


Moving Platforms:

#pragma strict

public var heightLimit :float;
public var lowLimit :float;
public var leftLimit :float;
public var rightLimit :float;
public var moveUpAndDown :boolean;
public var moveLeftAndRight :boolean;
public var moveUp :boolean = false;
public var moveDown :boolean = false;
public var moveLeft :boolean = false;
public var moveRight :boolean = false;
public var speed :float;

function Update ()
{
if (moveLeftAndRight) // Horizontal movement
{
if (transform.localPosition.x <= leftLimit)
{
moveLeft = false;
moveRight = true;
}
else if (transform.localPosition.x >= rightLimit)
{
moveLeft = true;
moveRight = false;
}
//Move platform back and forth
if (moveRight)
transform.localPosition.x += speed * Time.deltaTime;
else if (moveLeft)
transform.localPosition.x -= speed * Time.deltaTime;


}
if (moveUpAndDown) // Vertical movement
{
if (transform.localPosition.y <= lowLimit)
{
moveUp = true;
moveDown = false;
}
else if (transform.localPosition.y >= heightLimit)
{
moveDown = true;
moveUp = false;
}
// Move platform Up and Down
if (moveUp)
transform.localPosition.y += speed * Time.deltaTime;
if (moveDown)
transform.localPosition.y -= speed * Time.deltaTime;
}
}

Similar to the timer you just need to edit in the Unity Editor once you have attached it to the object.
Tick the movement method you want and put the axis where you want the platform to change direction in the correct limit. Note; You must tell it what direction to start moving on one of the bottom four tick boxes.


Hope this stuff helps and feel free to use them.

No comments:

Post a Comment