- Home /
Crash at regeneration script
var gotHitTimer=0; var regentimer=0; var hurtsound : AudioClip; var waittime=5; var healthlastframe :float = 100; var walkSpeed = 6.0; var respawnpoint : GameObject; var runSpeed = 11.0; var dietimes = 0;
var limitDiagonalSpeed = true; var shadap : AudioClip;
var toggleRun = false; var health :float =100; var jumpSpeed = 8.0; var gravity = 20.0;
var fallingDamageThreshold = 10.0;
var slideWhenOverSlopeLimit = false;
var slideOnTaggedObjects = false;
var slideSpeed = 12.0;
var airControl = false;
var antiBumpFactor = .75;
var antiBunnyHopFactor = 1;
private var moveDirection = Vector3.zero; private var grounded = false; private var controller : CharacterController; private var myTransform : Transform; private var speed : float; private var hit : RaycastHit; private var fallStartLevel : float; private var falling = false; private var slideLimit : float; private var rayDistance : float; private var contactPoint : Vector3; private var playerControl = false; private var jumpTimer : int;
function Start () { controller = GetComponent(CharacterController); myTransform = transform; speed = walkSpeed; rayDistance = controller.height * .5 + controller.radius; slideLimit = controller.slopeLimit - .1; jumpTimer = antiBunnyHopFactor; oldPos = transform.position; }
function FixedUpdate() { var inputX = Input.GetAxis("Horizontal"); var inputY = Input.GetAxis("Vertical");
var inputModifyFactor = (inputX != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
if (grounded) {
var sliding = false;
if (Physics.Raycast(myTransform.position, -Vector3.up, hit, rayDistance)) {
if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
sliding = true;
}
else {
Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, hit);
if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit)
sliding = true;
}
if (falling) {
falling = false;
if (myTransform.position.y < fallStartLevel - fallingDamageThreshold)
FallingDamageAlert (fallStartLevel - myTransform.position.y);
}
if (!toggleRun)
speed = Input.GetButton("Run")? runSpeed : walkSpeed;
if ( (sliding && slideWhenOverSlopeLimit) || (slideOnTaggedObjects && hit.collider.tag == "Slide") ) {
var hitNormal = hit.normal;
moveDirection = Vector3(hitNormal.x, -hitNormal.y, hitNormal.z);
Vector3.OrthoNormalize (hitNormal, moveDirection);
moveDirection *= slideSpeed;
playerControl = false;
}
else {
moveDirection = Vector3(inputX * inputModifyFactor, -antiBumpFactor, inputY * inputModifyFactor);
moveDirection = myTransform.TransformDirection(moveDirection) * speed;
playerControl = true;
}
if (!Input.GetButton("Jump"))
jumpTimer++;
else if (jumpTimer >= antiBunnyHopFactor) {
moveDirection.y = jumpSpeed;
jumpTimer = 0;
}
}
else {
if (!falling) {
falling = true;
fallStartLevel = myTransform.position.y;
}
if (airControl && playerControl) {
moveDirection.x = inputX * speed * inputModifyFactor;
moveDirection.z = inputY * speed * inputModifyFactor;
moveDirection = myTransform.TransformDirection(moveDirection);
}
}
moveDirection.y -= gravity * Time.deltaTime;
grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
}
function Update () {
if (toggleRun && grounded && Input.GetButtonDown("Run")){
speed = (speed == walkSpeed? runSpeed : walkSpeed);
}
if (health<1){
audio.PlayOneShot(shadap);
transform.position = respawnpoint.transform.position;
health=100;
}
if(health<100){
regen();
}
if(health>100){ health=100; }
Camera.main.GetComponent("ColorCorrectionEffect").rampOffsetR = (100-health)/150;
if(health!=healthlastframe){ regenwait(); } }
function OnControllerColliderHit (hit : ControllerColliderHit) { contactPoint = hit.point; }
function FallingDamageAlert (fallDistance : float) { Debug.Log ("Ouch! Fell " + fallDistance + " units!");
if(fallDistance>20) health=health-fallDistance/1.5;
} function OnGUI () { GUI.Label (Rect (30, 80, 100, 30), health.ToString());
} function Awake(){ transform.position = respawnpoint.transform.position; } function regen(){
while(regentimer==1)
health+=0.5;
if(health>100){
health=100;
}
}
function regenwait(){ regentimer=0; playhurtsound(); addblur(); yield WaitForSeconds(5); regentimer=1; healthlastframe = health; }
function addblur(){ camera.main.GetComponent("BlurEffect").enabled=true; yield WaitForSeconds(1); camera.main.GetComponent("BlurEffect").enabled=false; }
function playhurtsound(){ if (Time.time > gotHitTimer) { audio.PlayOneShot(hurtsound); } gotHitTimer = Time.time + hurtsound.length;
}
@script RequireComponent(CharacterController)
first of all sorry for the huge script here. Okay i take damage and wait five seconds to regen and then the whole thing crashes. What could have gone wrong?
Answer by Mike 3 · Jun 10, 2010 at 06:16 PM
these lines:
while(regentimer==1)
health+=0.5;
you're literally pausing the execution of anything else in unity until regentimer is 1, but it'll never get to 1 because it's running the while
you'll want to do something like
while(regentimer==1)
{
health+=0.5;
yield;
}
which would increment the health once a frame, or better yet:
while(regentimer==1)
{
yield;
health+= regenSpeed * Time.deltaTime;
}
where regenspeed is the health per second you want to apply