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 Hephaestus1233 · Dec 01, 2018 at 08:12 PM · rigidbody2dscene-switching

RigidBody2D velocity not working after returning to Menu and going back to game.

I have a simple PONG game for my phone that I want to make. The pause button allows you to return to the menu and when you return to the menu you can return back to the game. The game and the menu are seperate Scenes.


However when you return back to the game, the ball is stuck in the middle and the velocity of the rigidbody isn't doing anything.

I can see the rigidbody2d's velocity not at 0,0 but it isn't doing anything to move the ball.

 public class BallBehaviour : MonoBehaviour {
     public TextMeshProUGUI rightScore;
     public TextMeshProUGUI leftScore;
 
     public GameObject lGoal;
     public GameObject rGoal;
     private BoxCollider2D right;
     private BoxCollider2D left;
     private int rAScore; //actual score
     private int lAScore;
     private Vector2 lasPos;
 
 
     // Use this for initialization
     void Start () {
         Random.InitState(Mathf.RoundToInt(Time.time));
         Reposition();
         right = rGoal.GetComponent<BoxCollider2D>();
         left = lGoal.GetComponent<BoxCollider2D>();
     }
     
     // Update is called once per frame
     void Update () {
         if (right.IsTouching(gameObject.GetComponent<CircleCollider2D>()))
         {
             //Debug.Log("right");
             rAScore++;
             rightScore.text = rAScore.ToString();
             Reposition();
         }else if (left.IsTouching(gameObject.GetComponent<CircleCollider2D>()))
         {
             //Debug.Log("left");
             lAScore++;
             leftScore.text = lAScore.ToString();
             Reposition();
         }
         StartCoroutine(ScanPos());
     }
 
     public void Reposition()
     {
         Random.InitState(Mathf.RoundToInt(Time.time));
         int r = Random.Range(1, 3);
         gameObject.GetComponent<Rigidbody2D>().MovePosition(new Vector3(0,0,0));
 
         if (r == 1)
         {
             gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(7, 5f);
         }
         else
         {
             gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(-7, -5f);
         }
     }
 
     IEnumerator ScanPos()
     {
         yield return new WaitForSecondsRealtime(0.1f);
         if (lasPos == null)
         {
             lasPos = gameObject.GetComponent<Rigidbody2D>().position;
         }
         else if (lasPos == gameObject.GetComponent<Rigidbody2D>().position)
         {
             Reposition();
         }
     }
 }

You can also see that I have tried to fix it with a coroutine, but I'm not too sure on how coroutines really work for Unity and I'm most likely not using the coroutine efficiently.

Comment
Add comment · Show 3
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 Hephaestus1233 · Dec 01, 2018 at 08:19 PM 0
Share

The coroutine isn't even working correctly so for the time being I have it commented out.

avatar image Vega4Life · Dec 01, 2018 at 08:30 PM 0
Share

Yeah, that coroutine isn't going to be any good. It's called every frame with a 0.1f delay. Basically, it's going to create a ton of those things and cause all kinds of chaos.


How are you pausing the game? Setting the Timescale? If you are, are you sure you're setting the timescale back to 1?

avatar image Hephaestus1233 Vega4Life · Dec 03, 2018 at 09:20 PM 0
Share

Yes, the pause menu is paused through time scale. That may be my problem, lemme check

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

97 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

Related Questions

OnCollision2D stopped working 1 Answer

Optimizing C# Code and physics 2 Answers

Child object not moving with parent. 2 Answers

Trying to make a bounce platform that tosses the player. 1 Answer

Transform.forwrard of a Sprite? 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