Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 kayrayorulmaz · Apr 01, 2021 at 10:17 AM · 2dbeginnerlerpgridlerping

Why won't my blocks fall?

In my grid-based blocks game I had trouble with the falling code for the game's blocks.

Here's the code snippet:

 void Update()
     {
         for (int i = (int)transform.position.y; i >= 0; i--)
         {
             if (transform.position.y >= 0 && SpawnBlocks.allBlocks[(int)gameObject.transform.position.x, i] == null)
             {
                 StartCoroutine(LerpPosition(new Vector2(transform.position.x, i), 1));
             }
         }
 
         
     }
 
 
 IEnumerator LerpPosition(Vector2 targetPosition, float duration)
     {
         float time = 0;
         Vector2 startPosition = transform.position;
 
         while (time < duration)
         {
             transform.position = Vector2.Lerp(startPosition, targetPosition, time * duration);
             time += Time.deltaTime;
             yield return null;
         }
         transform.position = targetPosition;
 
         SpawnBlocks.allBlocks[(int)transform.position.x, (int)transform.position.y] = gameObject;
         gameObject.name = "(" + transform.position.x.ToString() + "," + transform.position.y.ToString() + ")";
     }

Each block checks through a list to see whether there is a block beneath it. If there isn't, then it should smoothly lerp down that many blocks to fill it in.

There are three problems occurring however:

  1. Blocks do not fall down to the lowest row

  2. Even after one block has fallen down, blocks above that block in the same column won't go down and will be stuck up there.

  3. If there is only a single space between blocks, then the block above will only move .016694 rather than a full 1

  4. Instead of actually Lerping, blocks would just appear at the space beneath.

I do not know why the blocks act like this.

Here's an example pic of what is happening: alt text

And here's an edited version showing what should happen: alt text

i2.png (19.6 kB)
i1.png (17.6 kB)
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 DrTomato · Mar 30, 2021 at 06:12 PM

Your use of Lerp is incorrect. The third parameter, where you have passed in timeElapsed needs to a value between 0 and 1. You can think of it as a percentage of the required movement. At 0 the position will at the start (initPos). At 1 the position given will be the object final position (targetPos). At 0.5 it will be exactly halfway between initPos and targetPos. In your sample, you declare a float variable and set it to zero each time you iterate in your loop, and then add Time.deltaTime * 4 (or roughly the time taken for 4 frames) so you will always get a small number for your timeElapsed (close to 0) , resulting in a position very near to initPos.

I don't know how the code you supplied is being called but I find Lerp is easiest to use in a coroutine. There are some examples of how it is used here https://gamedevbeginner.com/the-right-way-to-lerp-in-unity-with-examples/#lerp_examples

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 kayrayorulmaz · Mar 31, 2021 at 02:27 AM 0
Share

However, even when I have changed the code to be a coroutine (code update above), blocks still lerp to a position very near initPos but not exactly. Moreover, now blocks begin to fall into each other rather than stand on top of.

avatar image kayrayorulmaz · Mar 31, 2021 at 02:46 AM 0
Share

UPDATE: I was able to make the blocks fall down now, but if there is a block above any other block, they fall into each other.

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

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

Lerp is not moving 2D Prefab over time. 2 Answers

Lerp not moving 2D object correctly 1 Answer

Lerping in 2D 1 Answer

Seeking tutorial for bejeweled -like game? 1 Answer

How to have a grid detect which square a player is in, in unity 2D 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