- Home /
Controlling One of Several Similar Playing Pieces
hello, I'm trying to create a little tactical squad shooter where you control your squad one man at a time.
Story so far: I've made a prefab for the basic squad member.
I've made a Controller script which is attached to the Main Camera. It finds all the team members (using tags) and when I right click, the camera moves to the next squad members position and activates the mouselook and FPSWalker components.. so this allows you to control each team member one at a time.
The Problem: I'm now wanting to introduce some more complicated behaviour, such as, set a limit on how long each piece can be controlled for. So I've added a script to each SquadMember GameObject which contains:
static var MoveTime : int; var moveTime : int;
function Start () { MoveTime = moveTime; }
function Update () { }
So then using the Unity interface, I've set one Squad member's moveTime to 3 seconds, and one to 6 seconds. The Controller script is then meant to read this information and use it to control how long each man can be moved for:
// get all playing pieces in a gameobject array friendlyPieces = GameObject.FindGameObjectsWithTag("RedPlayer");
/... some code I've missed out which updates activePiece when I click the mouse... /
currentPiece = friendlyPieces[activePiece]; // move camera transform.position = currentPiece.transform.position; transform.rotation = currentPiece.transform.rotation; transform.parent = currentPiece.transform; // enable player control currentPiece.GetComponent(MouseLook).enabled = true; currentPiece.GetComponent(FPSWalker).enabled = true; currentPieceMoveTime = currentPiece.GetComponent(PlayerStats).MoveTime
However, this doesn't seem to work. Although the camere correctly moves to a new tagged GameObject when I update activePiece, it always reports the same currentPieceMoveTime.
Any suggestions? Am I right in thinking I need to learn how to use classes??
Thanks
Answer by qJake · Mar 22, 2010 at 09:23 PM
Did you count down your moveTime when the piece is active?
This isn't line-for-line code with your script, but you'd want to do something like this:
function Update()
{
if(isActivePiece)
{
// Subtracts time in seconds. deltaTime is (float) time
// in seconds since last Update() call.
moveTime -= Time.deltaTime;
}
}
It doesn't look like you have the "isActivePiece" bool, so you'll need to create that yourself.
Your answer
Follow this Question
Related Questions
How to differentiate between players in multiplayer game 1 Answer
How to make classes? 2 Answers
Editting a custom javascript class in the inspector 1 Answer
Using AddComponent to add a Sub Class using a String 4 Answers
Where is the best place to host a class that is not intended to be a MonoBehaviour or Component? 1 Answer