Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mbhigham · Nov 04, 2016 at 12:03 AM · triggertimertriggerstime.deltatimetimer-script

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();
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.
 }
 }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mbhigham · Nov 04, 2016 at 12:35 AM 0
Share

I have no idea how to do c#, I'd be extremely grateful if you could put it into javascript

avatar image TheEpicStache_77 · Aug 05, 2020 at 06:09 PM 0
Share

@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);

 }` 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to slow down my timer script? 0 Answers

I need my timer to only start after it hits a trigger. Please help! 0 Answers

Milliseconds Timer Question 1 Answer

How is it possible to verify values and is they are correct activate objects ? 0 Answers

Timer System in GameManager with two lists 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges