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 /
avatar image
0
Question by dragonballcreed · Oct 01, 2017 at 11:39 PM · 2djavascript

how to only allow a script to run once every X amount of given time unity

I'm new to unity and coding in general here is my code so far. I'm not sure whats wrong if i could get some help that would be great. i'm trying to make a 2d strategy game where I can move once within the space of 5 seconds. the script is called PlayerController.

 #pragma strict
 var ph : PlayerController;
 private var speed = 10;
 private var pos : Vector3;
 private var tr : Transform;
 function Start() 
 {
      pos = transform.position;
      tr = transform;
      ph = GetComponent(PlayerController);
      ph.enabled = false;
 }     
 function Update() 
 {
      if (Input.GetKey(KeyCode.D) && tr.position == pos)
      {
          pos += Vector3.right;
      }
      else if (Input.GetKey(KeyCode.A) && tr.position == pos)
      {
          pos += Vector3.left;
      }
      else if (Input.GetKey(KeyCode.W) && tr.position == pos)
      {
          pos += Vector3.up;
      }
      else if (Input.GetKey(KeyCode.S) && tr.position == pos)
      {
          pos += Vector3.down;
      }
  
      transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed); 
 }
 
 
 function OnlyMoveOnce(runTime : float) {
  var timer = 5;
      while (timer < runTime) {
          ph.enabled = true;
          timer += Time.deltaTime;
          yield;
      }
  }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Oct 02, 2017 at 11:03 AM

Good day @dragonballcreed !!

I recommend you to create a bool variable called "CanMove". To allow movement, CanMove must be true. When the player executes a movement, this CanMove variable becomes false, and activate a timer for 5 seconds. When the timer reaches 5 seconds the CanMove variable becomes true again.

To make a timer you need Time.deltaTime. You can use this code (is in C#, but I'm sure you will understand it) :

 void Update()
  {
  private float MovementTimer = 0;
  private float MaxTimeForNextMove = 5;
  if(CanMove == false && MovementTimer < MaxTimeForNextMove )
   {
   MovementTimer += Time.deltaTime
   }
  if (MovementTimer >= MaxTimeForNextMove )
   {
   CanMove = true;
   MovementTimer = 0;
   }

This way, the timer will start counting when you change the CanMove variable to false. When the timer reaches 5 seconds, it goes 0 again and allow player to move.

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

170 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 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

2d camera help! 0 Answers

random sprite javascript 0 Answers

How would I make a route that shows where player moves beforehand? 0 Answers

Changing material orientation dependant on mouse location? 0 Answers

2D Smoke or Particle 2 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