- Home /
Use a script more than once?
Hello Unity3D i have a question about using the same script for more than one character?Is it possible to use the same script for more than one player?the reason why i ask this question is the fact that i have more than one character that needs to do combos and so far i only got one character to do so but i want another one to do it as well.If anyone know i can fix this problem.Can you please tell me how can i?
Script if anyone was wondering #pragma strict
var combos : combosequence[] = new combosequence[3]; //Combo class. Fill this out in Inspector.
var comboMaxTime : float = 1.0; //How long a timer for combo lasts in seconds. Fill this out in Inspector.
var comboSpanTime : float = .25; //The span of when a combo can start. Fill this out in Inspector.
var comboMidPoint : float = .5; //The position within the maxtime of a combo the span is active. Fill this out in Inspector.
private var currentCombo : int = -1;
private var comboTimeout : float = .0;
AudioListener.volume = 100;
function Update () {
if (Input.GetKeyDown("u")) Attack();
if (comboTimeout > 0) DecreaseTime();
}
function Attack () {
ComboTime(); //Do all the logic for timing before animation etc.
animation.Play(combos[currentCombo].comboAnimation); //Play the currentCombo combos' animation
audio.clip = combos[currentCombo].comboSound; //Set the audio clip to the currentCombo combos' audio
audio.Play(); //Finally play the audio
audio.volume = 50;
//Add logic for hitting enemies etc here!
}
function ComboTime () {
if (currentCombo<combos.Length-1 &&
comboTimeout>0 &&
comboTimeout>comboMidPoint-comboSpanTime &&
comboTimeout<comboMidPoint+comboSpanTime ||
currentCombo==-1) {
currentCombo++;
} else {
currentCombo = -1;
}
comboTimeout = comboMaxTime;
}
function DecreaseTime () {
comboTimeout-=1*Time.deltaTime;
}
class ComboSequence {
var comboName : String; //A name for current attack (might be of use later for tutorial or something else)
var comboAnimation : String; //The animation name for current attack
var comboSound : AudioClip; //The audio for current attack
}
Answer by Kiwasi · Aug 21, 2014 at 04:52 AM
Yes. Its common practice to reuse scripts. Otherwise no one would finish anything. Couple of points to note.
Put your input in a separate component from your engine. That way your player(s) and ai can both use the same engine code.
Don't hardcode anything, write every thing in terms of variables that can be adjusted
Keep each script to a single task