Skip to content
December 27, 2013 / CoffeeCoder

Having WAY too much fun with Unity…

I am having a ridiculous amount of fun with Unity tonight.

First, I’ve been working on creating “elevators” (or just moving platforms) that move to specific targets, wait a few seconds, then travel back the way that they came.  It was great fun getting that to work!

I know it’s pretty simple stuff, but keep in mind that I hadn’t been programming in quite a while so had forgotten a lot about what I had learned in the past.  Now, though, I’ve learned so much more it’s kind of nuts.

This is the final script I wrote to control the elevators:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour 
{
	// Public variables
	public Vector3 origin;                // Origin point of platform (can be set in inspector)
	public Vector3 target;                // Target point of platform (can be set in inspector)
	public float speed;                   // Speed to move at (can be set in inspector)
	public float timeDelay;               // Time to rest before moving again (can be set in inspector)
	public bool  moveSwitch;

	// Private variables
	private bool moving;     	          // Determines if the platform is moving or not
	private Vector3 newTarget;            // Sets target of platform after it's reached its original target
	private float timeDelayBackup;        // Used to reset timeDelay
	private Transform myTransform = null; // Used to cache transform

	// Initialize variables
	void Start()
	{
		newTarget = target;                      // Sets the initial target to the one specified in the inspector
		timeDelayBackup = timeDelay;             // Backs-up the value of timeDelay so it can be reset
		moving = true;                           // Set moving to true (though the timer still counts down the first time the script is run)
		myTransform = GetComponent<Transform>(); // Caches transform in myTransform
		myTransform.position = origin;           // Sets position to origin point set in inspector
		moveSwitch = false;                      // Sets moveSwitch to false
	}

	// Use FixedUpdate to ensure accurate movement
	void FixedUpdate() 
	{
		// Only move if moveSwitch is true
		if (moveSwitch)
		{
	     	    // When elevator meets end position, reverse direction and set moving to false
			if (myTransform.position == target)
			{
				newTarget = origin;
				moving = false;
			}else if (myTransform.position == origin)
			{
				newTarget = target;
				moving = false;
			}

			// Reset timer when it hits 0f and set moving to true
			if (timeDelay <= 0f)
			{
				timeDelay = timeDelayBackup;
				moving = true;
			}

			// Move platform when moving is true
			if (moving)
			{
				myTransform.position = Vector3.MoveTowards(myTransform.position, newTarget, Time.fixedDeltaTime * speed);
			}else
			{
				// Begin countdown and halt movement
				timeDelay -= Time.fixedDeltaTime;
			}
		}
	}
}

As you can see, though, I didn’t stop there.  I created this variable called “moveSwitch”, which only moves the elevator when it’s set to true.  Which it’s not, so how did I do that?!

I created another object and named it “ElevatorSwitch”, then I gave it this script!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ElevatorSwitch : MonoBehaviour 
{
	// Declare variables
	public List<GameObject> movingPlatforms = new List<GameObject>(); // Stores assigned platforms (used in inspector)
	private MovingPlatform script;                                    // Used to grab the script component from each platform

	// Update Loop
	void Update()
	{
		//Flip switch between on and off
		if (Input.GetKeyDown(KeyCode.Q))
		{
			// Loop through each platform and flip the value of moveSwtich
			for(int i = 0; i < movingPlatforms.Count; i++)
			{
				script = movingPlatforms[i].GetComponent<MovingPlatform>();
				script.moveSwitch = !script.moveSwitch;
			}
		}
	}	
}

So once I had done that, all I had to do was assign each elevator to its slot in the list, and the script would iterate through the list for how many elevators I put in, and whenever I press the “Q” key, it toggles between on and off.  Wahlah!

The plan is to use these two basic ideas and create 2 prefabs – a switch and the elevator itself.  The idea is to be able to link certain groups of elevators to certain switches only – so for example, you can have a red group and a blue group, each with red and blue switches.  When you activate the blue switch, the blue group moves accordingly!  When you activate the red switch, the red group moves.  Really simple!

One way to take this further is to deactivate the other group of elevators when another group is activated, which would add a real puzzle element, wouldn’t it?  Perhaps that’s the evil plan!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: