- Home /
Weird physics issue after disabling and re-enabling player gameobject.
Hey everyone,
So I've got this problem where, if I disable and then re-enable my player object at any time, upon re-enabling, my player moves extremely slow along the X axis (2D game). All of the movement variables are the same, nothing has changed. I'm guessing it's some weird physics thing, but I honestly have no idea.
I want to disable the player so I can move it around the Scene without being seen. (scene/seen ha!) Right now I'm manually going through and disabling each component of the player in a script to make it "look" like it is disabled. This is pretty messy and I'd rather figure out the solution to this problem.
Couple of notes, this is a 2D game and the Y axis doesn't seem to be affected. I can still jump at normal height and speed.
Any help is much appreciated! Thanks!
$$anonymous$$aybe your player went to sleeping mode, so you could check if Rigidbody.IsSleeping() true than Rigidbody.WakeUp()
If you want to move player without being seen, you could try For 3D Renderer.enabled For 2D SpriteRenderer.enabled $$anonymous$$akes the rendered object visible if enabled.
Thanks for the suggestion, just checked and no, the rigidbody isn't going into sleeping mode.
I want to move the player and then hold the position for a few seconds so just disabling the sprite renderer won't work, because the player can still move. Also there's a particle system attached to the player so I would have to disable that separately too.
Answer by Majesteit · Jan 04, 2017 at 07:12 PM
Maybe it is a good idea to disable the component that renders your player? I think it is a sprite renderer in your case.
GetComponent<SpriteRenderer>().enabled = false;
$$anonymous$$aybe I should provide some more info. I can't just disable the sprite renderer because I am moving the player back to a checkpoint after death. I want to disable the player on death, move them to the checkpoint, do a little respawn effect/animation, and then reactivate the player.
If I just disable the sprite the player will still have control while waiting for the respawn.
For some reason when I disable and then re-enable, things get wonky.
Have you tried setting the velocity of the rigidbody to 0 and then disabling the player? $$anonymous$$aybe the velocity is stored and reapplies when activating the rigidbody. I'm not too sure if that's the case, but it's worth a try.
GetComponent<Rigidbody2D>().velocity = new Vector2.Zero;
Good idea, but nope, that didn't work either. It's so weird. I can go through and disable/enable each and every component via a script and it works fine, but if I disable/enable the GameObject I have problems.
Answer by plague_spreader · Jun 18, 2018 at 03:14 PM
Did you manage to solve this problem @jdovermy? I have the same problem in Unity 5.4.0b16 and whenever I deactivate and reactivate a ball in my pool game that ball slows down "too much" when I strike.
I honestly can't recall if I did, sorry. $$anonymous$$aybe try updating to a newer Unity version and see if it fixes itself? It was such a weird issue...
Answer by JonPQ · Jun 18, 2018 at 04:30 PM
It could be cos the physics runs off time... maybe you are generating a very large time-step by turning the object off and on again. Try instantiating a fresh player prefab instead. Or disabling the character render and input(& not physics update) components when resetting it.
Your answer
Follow this Question
Related Questions
Softbody physics with 2D sprites 1 Answer
2D Jump using "Rigidbody2D.AddForce" doesn't work. 1 Answer
Problem with Falling Platform Script 3 Answers