So atm my scripting level is still pretty low. I know how to make a whole script act within a time limit, then just disable it. What I wan’t to do it have it so that just have a part inside of my script be timed. In allot of RPG games you see “buffs.” They strengthen your character, but they only last for short periods of time. That is basically what I want to do. In the following script (yes it is very very messy atm) there is a sort of buff that allows your to control your movement in the air. Without it you stay at your initial velocity.
//Funtion Update Variables
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
var sparkfab : Transform;
//Meshes for character
var MeshControl = 0;
var Mesh1 : Texture;
var Mesh2 : Texture;
var Mesh3 : Texture;
var Mesh4 : Texture;
var Mesh5 : Texture;
var Mesh6 : Texture;
//------------------
static var powerjump = false;
static var dead = false;
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
{
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 1);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
var spark = Instantiate(sparkfab, transform.Find("SpawnPoint").transform.position, Quaternion.identity);
}
}
[b]//-------------------------------------
if(powerjump)
{
if(!controller.isGrounded)
{
if(Input.GetKeyDown("d"))
{
moveDirection.x = speed;
}
if(Input.GetKeyDown("a"))
{
moveDirection.x = -speed;
}
}
}
//----------------------------------------[/b]
if(dead)
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
//-----------------------------------------
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
//Meshes Changers
switch(MeshControl)
{
case 0:
renderer.material.mainTexture = Mesh1;
break;
case 1:
renderer.material.mainTexture = Mesh2;
break;
case 2:
renderer.material.mainTexture = Mesh3;
break;
case 3:
renderer.material.mainTexture = Mesh4;
break;
case 4:
renderer.material.mainTexture = Mesh5;
break;
case 5:
renderer.material.mainTexture = Mesh6;
dead = true;
break;
}
}
function OnTriggerEnter( hit : Collider )
{
if(hit.gameObject.tag == "Crate")
{
MeshControl += 1;
}
}
So what I want to do is make it so that the “powerjump” only lasts for “x” amount of minutes (or seconds depending on the script I guess). How would I go about doing this?
when ‘buff’ happens, set a variable to the time it will last in seconds and decrement it by deltaTime each Update. When it is zero or below, buff is over.
if (powerjump > 0) {
powerjump -= Time.deltaTime; // this is the same as powerjump = powerjump - Time.deltaTime
...do stuiff here
}
Not sure if I get this, is there a simpler way to do this? Like creating another variable called “bufftime” and have it last for however long that is set to?
Also when I tried this I get an error, it says error because the powerjump is already a variable for me and you can’t do a boolean after the variable.
in my example powerjump is a float, not a boolean.
//Funtion Update Variables
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
var sparkfab : Transform;
//Meshes for character
var MeshControl = 0;
var Mesh1 : Texture;
var Mesh2 : Texture;
var Mesh3 : Texture;
var Mesh4 : Texture;
var Mesh5 : Texture;
var Mesh6 : Texture;
//------------------
static var powerjump = false;
static var dead = false;
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
{
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 1);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
var spark = Instantiate(sparkfab, transform.Find("SpawnPoint").transform.position, Quaternion.identity);
}
}
//Powerup that allows you to move while in the air
if(powerjump)
{
if(!controller.isGrounded)
{
if(Input.GetKeyDown("d"))
{
moveDirection.x = speed;
}
if(Input.GetKeyDown("a"))
{
moveDirection.x = -speed;
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
moveDirection.x = speed;
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
moveDirection.x = -speed;
}
}
}
//Future power up (maybe the ability to fly!)
//--
//Oh no you lost all your Health!
if(dead)
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
//Meshes Changers
switch(MeshControl)
{
case 0:
renderer.material.mainTexture = Mesh1;
break;
case 1:
renderer.material.mainTexture = Mesh2;
break;
case 2:
renderer.material.mainTexture = Mesh3;
break;
case 3:
renderer.material.mainTexture = Mesh4;
break;
case 4:
renderer.material.mainTexture = Mesh5;
break;
case 5:
renderer.material.mainTexture = Mesh6;
dead = true;
break;
}
}
function OnTriggerEnter( hit : Collider )
{
if(hit.gameObject.tag == "Crate")
{
MeshControl += 1;
}
}
So how can I do it while its a variable like in this script.
- why are you making powerjump static? It will work, just wondering.
(check for syntax errors, I’m a C# guy)
[COLOR="slategray"]//static var powerjump = false;[/COLOR] in this example we don't need this
var powerjump_max_time : float = 8; //meaning 8 seconds is the max time you ever can have
var powerjump_time_left : float;
function Update() {
//your stuff here
//if the power button is pressed
if(Input.GetButton ("Jump"))
{
//decrease time left when holding the powerjump button
powerjump_time_left[COLOR="red"] -= [/COLOR]Time.deltaTime;
//do we have any power left?
if (powerjump_time_left > 0) {
//do your powerjump bonuses in here
}
} else {
//we aren't using it, recharge when not using it
powerjump_time_left [COLOR="red"]+=[/COLOR] Time.deltaTime;
//never give the player a reserve of more than max_time (8 secs in this example)
powerjump_time_left = Mathf.Min(powerjump_time_left , powerjump_max_time);
}
Its static because i am telling it to be true from a different script. I will test your thing in a min, I gtg.
I seem to have allot of trouble understanding these scripts being posted, is anyone fluent in Java script and can make a really easy one to understand?
Heres a far simpler way: (hopefully you’ll understand it)
var buffTime : float=2;
var buff : boolean=false;
function Update () {
if (Whatever happens to make your buff) {
buff = true;
Invoke("EndBuff", buffTime);
}
if (buff) {
whatever your buff does
}
}
function EndBuff () {
buff = false;
}
You don’t need it to be static. You can access public variables via other scripts as well using GetComponent(ScriptName) on the GameObject.
I ended up using a variable that counted down, and after it was less than or equal to 0 then it stops working. This is also good because I can easily display how much time is left by just putting the variable as a label with GUI.
This is my script so far if you want to see it: (I cleaned it up allot)
//Funtion Update Variables
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
var sparkfab : Transform;
//Meshes for character
var MeshControl = 0;
var Mesh1 : Texture;
var Mesh2 : Texture;
var Mesh3 : Texture;
var Mesh4 : Texture;
var Mesh5 : Texture;
var Mesh6 : Texture;
//------------------
static var powerjump = false;
static var powerfly = false;
static var powershield = false;
//------------------
static var dead = false;
static var timecount = 20;
function Start ()
{
InvokeRepeating("Counter", 1, 1);
}
function Counter ()
{
timecount--;
}
function Update()
{
//Continually time down the Powerup Time limit
//--
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
{
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 1);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
var spark = Instantiate(sparkfab, transform.Find("SpawnPoint").transform.position, Quaternion.identity);
}
}
//Powerup that allows you to move while in the air
if(powerjump)
{
if(!controller.isGrounded)
{
if(Input.GetKeyDown("d"))
{
moveDirection.x = speed;
}
if(Input.GetKeyDown("a"))
{
moveDirection.x = -speed;
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
moveDirection.x = speed;
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
moveDirection.x = -speed;
}
}
if(timecount <= 0)
{
powerjump = false;
PowerUpGUI.powerupTF = false;
}
}
//Low Gravity Power up (lets just call it fly)
if(powerfly)
{
gravity= 5;
if(!controller.isGrounded)
{
if(Input.GetKeyDown("d"))
{
moveDirection.x = speed;
}
if(Input.GetKeyDown("a"))
{
moveDirection.x = -speed;
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
moveDirection.x = speed;
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
moveDirection.x = -speed;
}
}
if(timecount <=0)
{
powerfly = false;
PowerUpGUI.powerupTF = false;
}
}
if(!powerfly)
{
gravity = 20;
}
//Shield recharger!
if(powershield)
{
if(MeshControl <= 5)
{
MeshControl = 0;
}
if(GUIH.Health < 6)
{
GUIH.Health += 1;
}
if(timecount <= 0)
{
powershield = false;
PowerUpGUI.powerupTF = false;
}
}
//Oh no you lost all your Health!
if(dead)
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
//Meshes Changers
switch(MeshControl)
{
case 0:
renderer.material.mainTexture = Mesh1;
break;
case 1:
renderer.material.mainTexture = Mesh2;
break;
case 2:
renderer.material.mainTexture = Mesh3;
break;
case 3:
renderer.material.mainTexture = Mesh4;
break;
case 4:
renderer.material.mainTexture = Mesh5;
break;
case 5:
renderer.material.mainTexture = Mesh6;
dead = true;
break;
}
}
function OnTriggerEnter( hit : Collider )
{
if(hit.gameObject.tag == "Crate")
{
MeshControl += 1;
}
}
That’s pretty much equivalent to what I was showing, but missing the recharge step. Glad you got it sorted.