Trigger a Timer
I need to get my script to enable when I walk in a trigger, the time to start when I enter the trigger.
It starts whenever I launch the game.
#pragma strict
var startTime : float;
var score : int;
var scoreText : UnityEngine.UI.Text;
function OnTriggerEnter (player : Collider)
{
startTime=Time.time;
}
function Update ()
{
score = Time.time;
}
function CalculateScore () : int
{
return Time.time;
}
function OnGUI ()
{
scoreText.text = "Time Score: " + " " + score.ToString();
}
Answer by Cynikal · Nov 04, 2016 at 12:20 AM
Note: I'll write this out in C#, but shouldnt be hard for you to convert.
bool TimerStarted = false;
void OnTriggerEnter (blah blah)
{
if (!TimerStarted) TimerStarted = true;
}
private float _timer = 0f;
public float TimeIWantInSeconds = 10f;
void Update()
{
if (TimerStarted)
{
_timer += Time.deltaTime;
if (_timer >= TimeIWantInSeconds)
{
//Do whatever since the time has elapsed.
}
}
}
I have no idea how to do c#, I'd be extremely grateful if you could put it into javascript
@Cynikal Umm, what goes in the parentheses on "void OnTriggerEnter (blah blah)"? unity keeps telling me that "The type or namespace'blah' could not be found" . I'm trying to use this timer to put a limit on a sprint function in a character controller. here's the code:`
using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { //public public CharacterController controller;
//Speed Controller floats
public float $$anonymous$$oveSpeed = 10f;
public float WalkSpeed = 10f;
public float SprintSpeed = 24f;
//Physics floats
public float gravity = -9.81f;
public float jumpHeight = 3f;
//Groaund check floats/public
public Transform groundCheck;
public float groundDistance = 0.4f;
public Layer$$anonymous$$ask ground$$anonymous$$ask;
//Vectors, Variables and Bools
Vector3 velocity;
bool isGrounded;
bool sprinting = false;
bool TimerStarted = false;
void SprintTrigger(blah blah)
{
if (!TimerStarted) TimerStarted = true;
}
//Timer
private float _timer = 0f;
public float TimeIWantInSeconds = 10f;
//Commands that run during the entire game
void Update()
{
// Sprint timer data
if (TimerStarted)
{
_timer += Time.deltaTime;
}
if (_timer >= TimeIWantInSeconds)
{
$$anonymous$$oveSpeed = SprintSpeed;
}
//Gravity, Physics
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, ground$$anonymous$$ask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -4f;
}
// $$anonymous$$ovement
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
//Sprinting Command
if (Input.GetButtonDown("Fire3"))
{
void SprintTrigger(blah blah);
}
else if (Input.GetButtonUp("Fire3"))
{
$$anonymous$$oveSpeed = WalkSpeed;
}
if (controller.isGrounded && sprinting)
{
$$anonymous$$oveSpeed = SprintSpeed;
}
// $$anonymous$$ovement
controller.$$anonymous$$ove(move * $$anonymous$$oveSpeed * Time.deltaTime);
//Jumping
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = $$anonymous$$athf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.$$anonymous$$ove(velocity * Time.deltaTime);
}`
Answer by SwimmingBird · Nov 06, 2016 at 06:53 PM
Here's a tool that might be useful: https://www.assetstore.unity3d.com/en/#!/content/72989