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 Piflik · Sep 11, 2011 at 01:33 PM · javascriptrealtimeturn-based

Realtime Turn Based game, random movement?

I have a little problem with a random movement function in a (what I call) realtime turn-based game (the player moves in realtime, but every action is considered a turn and the enemies will move at the same time. If the player doesn't move, nothing else will). All on a Hex-Grid.

I have a script attached to the player that will set a turnActive variable 'true' for a set amount of time and this variable is used to determine if other objects can move or not. It does work up to here, but I now wanted to have some random movement for the enemies that are not aware of the player, but I want them to not change direction during one turn, only between two turns. I thought I could use the counter from the player's script to create a random number at each turn, but it doesn't work, no matter if I use '== 0' or '== turnDuration' in my if Statement. It does work if I use '== turnDuration -1', but then I have erratic movement for the last 1/15 of a turn.

statics.js:

 static var playerActive = false;
 static var turnDuration = 15;
 static var turnActive = false;
 static var currentDuration = 0;

Player.js:

 function Update () {
     if(Input.anyKey) {
         statics.playerActive = true;
     }
     if(statics.playerActive) {
         statics.turnActive = true;
         statics.playerActive = false;
     }
     
 }    
 
 function FixedUpdate () {
     if(statics.turnActive) {
         statics.currentDuration += 1;
         
         if(statics.currentDuration == statics.turnDuration) {
             statics.turnActive = false;
             statics.currentDuration = 0;
         }
     }
 }


BasicEnemyBehavior.js:

 var speed = 1;                  //number of tiles per turn
 private var heading;
 private var ranNmbr = 0;
 private var r = 1;
 private var g = 0.5;
 private var b = Mathf.Sqrt(3)/2;
 
 function FixedUpdate () {    
     if(statics.turnActive) {
         ranMove();
     }
 }
 
 function ranMove() {
     if(statics.currentDuration == 0) {
             ranNmbr = Random.Range(0, 6);
         }        
         switch(ranNmbr) {
             case(0): heading = Vector3(r, 0, 0);
             break;
             
             case(1): heading = Vector3(g, 0, b);
             break;
             
             case(2): heading = Vector3(g, 0, -b);
             break;
             
             case(3): heading = Vector3(-r, 0, 0);
             break;
             
             case(4): heading = Vector3(-g, 0, b);
             break;
             
             case(5): heading = Vector3(-g, 0, -b);
             break;
         }
         
         transform.Translate(heading * speed);
 }


Edit: I pulled the random number generator out of ranMove and into FixedUpdate (before the if Statement) and now it seems to work. But I now have a new Porblem: I created a Prefab from my test enemy and added additional copies to the scene, but they move a little bit faster than the original and thus stray off the grid. I don't know if that is connected to the scripts, but I haven't changed anything else...I guess I have to create an absolute position for the movement instead of a relative one...

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Creating realtime shadows without unity pro? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Grid based movement collision detection 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

decimal to fraction (just display) 1 Answer


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