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 Triggerzz · May 04, 2019 at 11:38 AM · 2drigidbody2daddforcestopmovetowards

Spawned objects won't stop moving after instantiating. 2D Top-Down

     void SpawnCoins()
     {
         for (int i = 0; i < coinDrops; ++i) //Spawns an amount of coins
         {
 
             GameObject coin = Instantiate(coinPrefab, transform.position, Quaternion.identity) as GameObject;
 
 
             Vector3 targetPosition = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), 0);
             Rigidbody2D rb = coin.GetComponent<Rigidbody2D>();
 
             rb.AddForce(targetPosition * speed);
 
             Debug.Log("Adding force");
             if (Vector2.Distance(coin.transform.position, targetPosition < 1f) //this part isn't working
             {
                 rb.velocity = Vector2.zero;
                 Debug.Log("It's going through.");
             }
 
         }
     }

Also, tried:

             if (coin.transform.position == targetPosition) 
             {
                 rb.velocity = Vector2.zero;
                 Debug.Log("It's going through.");
             }

So, I'm spawning coins after an enemy dies. After the coins instantiate, I want them to burst out in random directions and move a small distance away, and then stop. I can get a set amount of coins to spawn and they do move in random directions...but they won't stop moving. I would like help to figure out how to stop the coins.

I've used a debug.log to check if the "if" statement was going through, and it wasn't.

I've also tried moveTowards, but the coins just teleport to the target position. I'm guessing this is because it's not being called in the Update function. But I'm not sure how to reference the instantiated coin gameobject outside of it's own function. When I made it public, I got errors.

Any help/suggestions would be very much appreciated!

spawn.png (26.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
1
Best Answer

Answer by Jack-Mariani · May 04, 2019 at 01:04 PM

I see 2 issues:

THE CHECK MUST RUN OVER TIME

This is happening because you call the method once and not over time.

Basically this is what happening:

  • Spawn

  • Addforce to rigidbody

  • Check the distance just in the same frame

  • End

This means the rigidbody had not the time to move away.

To solve this in the same script you can use a coroutine (otherwise you can add a monobehaviour to the spawned object and use its update loop).

THE CHECK MUST BE DONE AGAINST A POSITION

This:

 Vector3 targetPosition = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), 0);

Gives you a direction between (-1f,-1f) and (1f,1f). While I think you should check against a position that should be something like:

 PositionToCheck = targetPosition  + coin.transform.position.

SOLUTION

You may implement the coin as monobehaviour like this:

1 - Create the monobehaviour for the coin

 [RequireComponent(typeof(Rigidbody2D))]
 public class CoinStopper : MonoBehaviour
 {
     // --------------- DATA TO CHECK THE SPEED --------------- //
     //the position we want the coin to reach
     private Vector2 _positionToReach;
     //the rigidbody
     private Rigidbody2D _rb;
 
     public void SpawnCoin(Vector2 direction, float speed)
     {
         //first thing we calculate the position to reach
         _positionToReach = (Vector2) transform.position + direction;
         //caching the rigibopdy
         _rb = GetComponent<Rigidbody2D>();
         //add the force
        _rb.AddForce(direction * speed);
     }
     
     
     //we use fixed update because this is related to a physics logic
     private void FixedUpdate()
     {
         //we keep checking in the fixed update
         if(Vector2.Distance(transform.position, _positionToReach) > 1f)
             _rb.velocity = Vector2.zero;
     }
 }

2 - Add this monobehaviour to your Coin prefab.

3 - Update the spawner code with this

 void SpawnCoins()
     {
         for (int i = 0; i < coinDrops; ++i) //Spawns an amount of coins
         {
             //instantiate the coin
             GameObject coin = Instantiate(coinPrefab, transform.position, Quaternion.identity) as GameObject;
 
             //calculate the position
             Vector3     targetPosition = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), 0);
 
             //send the logic directly to the coin
             var coinStopper = coin.GetComponent<CoinStopper>();
             coinStopper.SpawnCoin(targetPosition, speed);                
         }
     }

------EDIT Changed from coroutine to update in a specific monobehaviour.

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 Triggerzz · May 05, 2019 at 12:38 AM 0
Share

Thanks so much for the help. I've adjusted the code, but only one of the coins stops sometimes. I also tried adding PositionToCheck replacing the targetPosition, but the same thing happens, and none of the coins manage to stop.

Here's what I tried, very tiny differences compared to the code you provided, there might be something I messed up with the code: void SpawnCoins() { for (int i = 0; i < coinDrops; ++i) {

             GameObject coin = Instantiate(coinPrefab, transform.position, Quaternion.identity) as GameObject;
 
             Vector2 targetPosition = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
 
             Vector2 PositionToCheck = targetPosition + (Vector2)coin.transform.position;
 
             Rigidbody2D rb = coin.GetComponent<Rigidbody2D>();
 
             rb.AddForce(targetPosition * speed);
 
             StartCoroutine(CheckDistance(rb, coin, targetPosition));
         }
     }
 
 
     private IEnumerator CheckDistance(Rigidbody2D rb, GameObject coin, Vector2 targetPosition)
     {
 
         Vector2 finalPosition = (Vector2)coin.transform.position + targetPosition;
 
         while (Vector2.Distance(coin.transform.position, finalPosition) < 1f)
             yield return null;
 
         rb.velocity = Vector2.zero;
     }

And here's with the PositionToCheck added, which I may have also hecked up:

     void SpawnCoins()
     {
         for (int i = 0; i < coinDrops; ++i)
         {
 
             GameObject coin = Instantiate(coinPrefab, transform.position, Quaternion.identity) as GameObject;
 
             Vector2 targetPosition = new Vector2(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
 
             Vector2 PositionToCheck = targetPosition + (Vector2)coin.transform.position;
 
             Rigidbody2D rb = coin.GetComponent<Rigidbody2D>();
 
             rb.AddForce(targetPosition * speed);
 
             StartCoroutine(CheckDistance(rb, coin, PositionToCheck));
         }
     }
 
 
     private IEnumerator CheckDistance(Rigidbody2D rb, GameObject coin, Vector2 PositionToCheck)
     {
 
         Vector2 finalPosition = PositionToCheck;
 
         while (Vector2.Distance(coin.transform.position, finalPosition) < 1f)
             yield return null;
 
         rb.velocity = Vector2.zero;
     }

avatar image Jack-Mariani Triggerzz · May 06, 2019 at 12:16 PM 0
Share

@Triggerzz I've just updated the answer using a monobehaviour, please let me know if that works.

avatar image Triggerzz Jack-Mariani · May 09, 2019 at 04:18 AM 0
Share

It works, thank you so much for the help! The only thing I changed was ins$$anonymous$$d of adding force:` _rb.AddForce(direction speed);` I set the velocity:`_rb.velocity = direction speed;` I felt like I learned a lot from this, so thanks again. It was such a big help.

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

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

Related Questions

Constant speed in 2d game. 1 Answer

Trying to add force to an object... is this done correctly? 0 Answers

Addforce for 2d game is not doing anything. 0 Answers

how to fire my player out of a Cannon? 0 Answers

Object Colliding while passing along another object . 0 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