I was following the TornadoTwins tutorial series on the worm game (here is their page), and I was making health packs that would give a life to you if you lost at least one. It works if there is only one health pack on the level, and I was having a hard time getting them to work. I'll give you the code from each script that has something to do with the health pack.
HealthPackControl: (the script attached to the health packs)
`
static var GetHealth = false;
function Update ()
{
if((GetHealth == true)&(HealthControl.LIVES < 3))
{
HealthControl.LIVES ++;
HealthControl.HITS = 0;
GetHealth = false;
transform.position.y = -1000;
}
else if((GetHealth == true)&(HealthControl.LIVES == 3))
{
GetHeath = false;
HealthControl.LIVES = 3;
HealthControl.HITS = HealthControl.HITS;
}
}
` HealthControl:(GUI)
`
var health1 : Texture2D;
var health2 : Texture2D;
var health3 : Texture2D;
var bodyPart1 : Transform;
var bodyPart2 : Transform;
static var LIVES = 3;
static var HITS = 0;
static var GAMEOVER = false;
function Update ()
{
print("Lives: "+LIVES+" Hits: "+HITS+" Turrets Destroyed "+TurretCollision.TurretsDestroyed+" Generators Destroyed: "+GeneratorControl.GeneratorsDestroyed+" Keys: "+KeyControl.KEYS);
switch(LIVES)
{
case 4:
LIVES = 3;
HealthPackControl.GetHealth = false;
break;
case 3:
guiTexture.texture = health3;
HealthPackControl.GetHealth = false;
break;
case 2:
guiTexture.texture = health2;
HealthPackControl.GetHealth = false;
break;
case 1:
guiTexture.texture = health1;
HealthPackControl.GetHealth = false;
break;
case 0:
//Game Over script here
GAMEOVER = true;
LIVES = 3;
break;
}
switch(HITS)
{
case 1:
bodyPart2.renderer.enabled=false;
break;
case 2:
bodyPart1.renderer.enabled=false;
bodyPart2.renderer.enabled=false;
break;
}
if(HITS >= 3)
{
LIVES -=1;
HITS = 0;
MoveAround.dead = true;
bodyPart1.renderer.enabled=true;
bodyPart2.renderer.enabled=true;
}
if(MoveAround.dead)
{
bodyPart1.renderer.enabled=true;
bodyPart2.renderer.enabled=true;
HITS = 0;
}
}
` MoveAround: (the script attached to the player which handles a lot more than just moving)
//Moving Around
var speed = 3.0;
var rotateSpeed = 3.0;
//Shooting
var bullitPrefab:Transform;
//Dying
static var dead = false;
//Getting Hit
var tumbleSpeed = 800;
var decreaseTime =.01;
var decayTime = 0.01;
static var gotHit = false;
private var backup = [tumbleSpeed, decreaseTime, decayTime];
//Map\Area Transfer
static var ChangeMap = false;
static var AreaTransfer = false;
function LateUpdate()
{
if(dead)
{
transform.position = Vector3(0,8.5,0);
gameObject.Find("Main Camera").transform.position = Vector3(0,6,-10);
dead = false;
}
if(gotHit)
{
if(tumbleSpeed < 1)
{
//we'er not hit anymore... reset & get back in the game!
tumbleSpeed = backup[0];
decreaseTime = backup[1];
decayTime = backup[2];
gotHit = false;
}
else
{
//we're hit! Spin our character around
transform.Rotate(0,tumbleSpeed * Time.deltaTime,0, Space.World);
tumbleSpeed = tumbleSpeed - decreaseTime;
decreaseTime += decayTime;
}
}
}
//function OnControllerColliderHit(hit:ControllerColliderHit)
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "fallout")
{
dead = true;
HealthControl.LIVES -= 1;
}
if(hit.gameObject.tag == "enemyProjectile")
{
gotHit = true;
HealthControl.HITS += 1;
Destroy(hit.gameObject);
}
if(hit.gameObject.tag == "Portal")
{
ChangeMap = true;
}
if(hit.gameObject.tag == "Key")
{
KeyControl.KEYS ++;
Destroy(hit.gameObject);
}
if(hit.gameObject.tag == "AreaTransfer")
{
AreaTransfer = true;
}
if(hit.gameObject.tag == "HealthPack")
HealthPackControl.GetHealth = true;
}
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Jump"))
{
var bullit = Instantiate(bullitPrefab, transform.Find("FireBallSpawn").transform.position, Quaternion.identity);
bullit.tag = "playerProjectile";
bullit.rigidbody.AddForce(transform.forward * 2000);
}
if(transform.position.y < -200)
{
dead = true;
HealthControl.LIVES -= 1;
}
}
@script RequireComponent(CharacterController)
Again, when there are multiple health packs, it adds the lives right, but I can't get it to move away.
As you can see, some of this stuff wasn't covered in TT's tutorials (not sure about the FPS one), and I am not a total newbie, but please give me an answer that a newbie would understand. I am sorry if this was posted before, I spent about two hours looking on Unity Answers and didn't find anything.
Thanks,
Dustin