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 BloodBTF · Aug 04, 2017 at 03:21 AM · distanceendless runnerupdate problemcheckpoints

Endless runner distance calculation problem

I have a bit of a problem: see, i want my endless runner game to spawn in a checkpoint(3d object) each time the distance (transform position- start position) is over 500 meters. I have a basic understanding and this is the script i wrote.

 Public int distance;
 Public int increment = 500;//value the distance needs to be over for a checkpoint to spawn.
 Public int counter;
 Public int current;
 
 Update()
 {
 Distance = (origin.position- transform.position ).magnitude
 
 counter = distance/increment;
 
 if (distance > increment)
 {
 Current++;
 }
 
 If (current > last)
 {
 Last = current;
 Print("dist" + (counter*100) + "meters);
 //this is where the checkpoint should spaen
 //this is also where the problem is.
 
 }
 
 
 }





The problem is, the print function gets called EVERY FRAME. i want it to print ONLY ON the first frame, and then cease it from printing until it needs to again. Any help is greatly appreciated

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 eskivor · Aug 04, 2017 at 04:06 AM

Wow it looks like you're trying something really weird, for something you can do for way more simple and more easy to understand.

1) Please display the real code you are using, not the shortened one (no code like this would works in Unity as it).

2) There are missing info in this example, "last" is not defined before it's used and does the "origin" or "transform" object moves ? We don't know. I suppose the transform moves (in another script).

3) By testing your code with debug logs, you could find by yourself where is the error, then fix it.

4) Stocking distance between moving objects in a int, that's weird.

5) I'll answer you with C#. I suppose that the script is on the player, which is moving and the "origin" is the spawnpoint of the player, which isn't moving.

Put this inside your class :

 //Don't forget to assign it
 public Transform origin;
 
 public float distanceBetweenCheckpoints = 500f;
 float currentDistanceToNextCheckpoint;
 int numberOfCheckpointsCrossed;

 //Don't forget to write some code to move your transform position
 void Update ()
 {
 //Calculate distance
 float totalDistance = Vector3.Distance (origin.position, transform.position);
 
 //Does a modulo operation
 currentDistanceToNextCheckpoint = totalDistance % (float) numberOfCheckpointsCrossed;
 
 //Check if the distance is superior than a certain amount
 if (currentDistanceToNextCheckpoint >= distanceBetweenCheckpoints);
     numberOfCheckpointsCrossed ++;
 Debug.Log ("Cross a checkpoint");
 }
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
avatar image
0

Answer by NoseKills · Aug 04, 2017 at 06:18 AM

 if (distance > increment)

After you have traveled over 500 units, this will be true every frame so what you are describing is expected behaviour.

You have to change your code a bit

 public int distance;
  public int increment = 500;//value the distance needs to be over for a checkpoint to spawn.
  public int counter;
  public int current;
  
 void Update()
  {
       distance = (int)(origin.position- transform.position ).magnitude
       // How many times 500 units you have travelled
       counter = distance/increment;

      // You have traveled 500 units more times than last frame
      if (counter > current)
      {
            // Remember the new amount
           current = counter;
           // do everything related to reaching a new 500 unit milepost here
      }
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

69 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

Related Questions

multiplication score with distance, infinite runner 2d 1 Answer

Endless runner distance 2 Answers

Tracking distance after Instantiate 2 Answers

Making a 2d "radar" for space game 2 Answers

dynamic rotation distance 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