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 ricarditomontserrat · Mar 16, 2020 at 06:40 PM · platformstuckplatformsmoving platform

Why is my moving platform getting stuck?

I'm trying to do a moving platform between an array of transforms and its positions. Here's the code (sorry if it's really obvious, but it's my first time trying a engine).

     public float speed = 1;
     public Transform[] positions;
     private string direction = "Towards";
     private int counter = 0;
 
     void Start()
     {
         if (positions.Length > 0 || positions == null)
             transform.position = positions[0].position;
     }
 
     void Update()
     {
         if (positions.Length < 2 || positions == null)
             return;
         if (counter < positions.Length - 1 && direction.Equals("Towards"))
             counter++;
         else if (counter > 0 && direction.Equals("Backwards"))
             counter--;
         if (counter == positions.Length - 1)
             direction = "Backwards";
         else if (counter == 0) 
             direction = "Towards";
         transform.position = Vector3.MoveTowards(transform.position, positions[counter].position, speed * Time.deltaTime);
 
     }
Comment
Add comment · Show 1
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 streeetwalker · Mar 16, 2020 at 09:22 PM 0
Share

I posted my answer. Beside the complexity that is making it difficult to follow the logic of the code, it is missing a critical test: it needs to wait until the animation has reached it's destination before switching directions. that what the magnitude of position a - position b does.

We get the distance between them and then test against some threshold, because positions are unlikely to be exactly equal, ever. The reasons are due to the facts of the animation process, and that positions have floating point components that almost never become exactly equal.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by streeetwalker · Mar 16, 2020 at 09:12 PM

@ricarditomontserrat, I don't know, but it is too complicated. Simplify it!

The following assumes position 0 moves to the left, and position 1 moves to the right, and transform.position is already on the right at the start of this code.

So well start out moving left first. If you want to start on the left and move to the right first, then set counter = 1 at the start:

 int counter = 0;

 void update() {    
     if (positions.Length < 2 || positions == null) {
          if( ( transform.position - positions[counter].position ).magnitude < 0.001 ){  // we reached the destination!
               counter = (counter + 1) % 2 // alternate between 0 and 1
           }
           transform.position = Vector3.MoveTowards(transform.position, positions[counter].position, speed * Time.deltaTime);
      }
 }

a note about my personal preference: I find code easier to follow if you let blocks of code return naturally without placing returns in the middle.

Comment
Add comment · Show 3 · 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 ricarditomontserrat · Mar 16, 2020 at 10:20 PM 0
Share

Thank you so much! It's kind of funny because I tried to make it readable but it became worst lol.

avatar image streeetwalker ricarditomontserrat · Mar 17, 2020 at 08:38 AM 0
Share

@ricarditomontserrat, Hey , I just created a problem by changing your logic to exit the if block naturally and line of code had a problem already:

   if (positions.Length < 2 || positions == null) {

That will throw an error if positions is null because it is checking for the length first. The way compound conditions work is: the first clause of the condition evaluated first and the program decides if it needs to evaluate the next clause from left to right. So if your object is null, trying to get the length first will cause an error.

Therefore, always check for null first in any compound condition.

To fix that, and the problem I caused, you should change this to:

   if( positions != null && (positions.length > 1 && positions.length < 2)

That way, if the first one is not true - it is null, then it will not try evaluate the next clause and it will skip the if block.

Sorry and hope it helps!

avatar image ricarditomontserrat streeetwalker · Mar 18, 2020 at 10:41 PM 0
Share

Yeah, I noticed that and changed it at first sight, but thanks anyway :D

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

200 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

What's the latest version of Unity that supports Android 4.2? 1 Answer

Rigidbody character cannot move once parented to platform 1 Answer

Can You Help Me Get Back The Like ... When You Start A New Project And then you got cubes and put stuff there and textures but how do i get that back? i lost it somehow 0 Answers

How do I make my FPS character move smoothly on a rigidbody platform? 1 Answer

#if UNITY_ANDROID doesn't exists in unity 5 3 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