Rigidbody.addforce after scene reload
I have a character with a rigidbody, when I swipe the screen to the left the character moves on the X axis from 0 to -1.2 and when swiping to the right it goes from 0 to 1.2. I am doing this with rigidbody.addforce
Vector3 move = new Vector3(Myplayer.transform.position.x - 605, 0, 0);
rigidbody.AddForce(move * 5);
This works exactly like how I need it to, problem is when the player dies and I reload the scene with scenemanager.loadscene and try to swipe it now seems to be adding more force than it should. it would go from 0 to more like -2 or 2 instead of -1.2 and 1.2.
Earlier I was getting errors where after the scene was reloaded the log says it can not find the rigidbody anymore, I fixed that by having it look for the character again and find the rigidbody at the start.
Not sure why scenemanger is causing these issue, never had these problems with other projects I have worked on. Not sure if anyone would know of a fix or a better way to move the player without going through objects. Thanks in advance
Answer by streeetwalker · Apr 14, 2020 at 05:56 AM
@yousifmessi, When you load a scene, every object that is not created through the editor is destroyed. Everything is returned to the Scene's original state as it was before you pressed the Run button - before any code executes.
So it depends what your code does to objects after you hit the run button - any changes you make, objects you instantiate, anything that your code does, you have to redo. When you reload a scene, you have to do everything exactly the same way and in the same order, or you won't get back to your "Start"
This is a clue to your problem:
Earlier I was getting errors where after the scene was reloaded the log says it can not find the rigidbody anymore, I fixed that by having it look for the character again and find the rigidbody at the start.
I'm not sure what you mean by, "having it look for the character again and find the rigidbody" - When your game runs the first time, how is the character "found" and what do you do to it with respect to it's Rigidbody component?
This issue of the character getting more force after you reload the scene is likely due to some property the of Rigidbody component of your character, such as Drag, is not set the same way the second time around - i.e. when you reload the Scene.
@streeetwalker thanks for the response I appreciate it. I am not instantiating anything through code or making changes to the character which is why I am not understanding why it would not go back to how the first scene is. If all the objects return to their original state, then the scripts attached to them runs again right? At least that is what I am seeing when adding a debug.log.
What I meant about finding the player's rigidbody again was that before I was just using a seralizefiled for the rigidbody and dragged it to the script in the editor, I then changed that to GameObject.Find("Player").GetComponent();
I thought the drag or some other property would be the issue as well, but I am not making any changes to that through the scripts so its staying the same when the scene is reloaded.
I tried reloading the scene right after i hit play so that I am not making any changes, and it did the same thing. Is there a way to see what exactly changes after reloading a scene, maybe through a log some where. Again, thanks for the reply.
@yousifmessi, If you have a public RigidBody variable in a script, and you have dragged an object with a RigidBody to that field in the Script Component's Inspector, and then Save the Scene before you play, it will retain it when you load the Scene. You won't loose it. ( Also note, that if the script is on a prefab, those never retain such references to objects - for example, components - irrespective of anything else that is done.)
And you should note if you don't Save the Scene, it is not going remember any of the properties you set on a RigidBody component ether.
Not saving the Scene before you run may be your problem - it sounds like a good fit.
If that is not the case, then something is changing, somewhere! You don't have any Awake or Start event handlers that alter your code?
If it is not the problem of saving the Scene, One thing you could to to try to track this down put some Debug.Log statements immediately after the game runs, and print some of the RigidBody properties like Drag, and then Reload the scene and see if the logs show the same values or not.
If those all show the same values between the first run and after reload, then start debugging some of the physics engine related variables, and time related variables.
@streeetwalker thanks for the help, I debugged everything related to the rigidbody and physics, nothing had changed. What I did not realize is, I was using an asset that had a static method that I referenced in one of my scripts that moved the player. Changed it from being static, and made a reference to the script and it worked properly. Thanks again.