Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problems implementing revive feature.

Discussion in 'Scripting' started by marekkula, May 28, 2019.

  1. marekkula

    marekkula

    Joined:
    Oct 11, 2016
    Posts:
    22
    Hi,
    I am working on a game in which you're flying as a mosquito. I've implemented simple ragdoll, so when you collide with any obstacle (walls, furniture, etc.) the mosquito falls down and player controls are disabled.
    While falling down the player has a chance to revive the mosquito by quickly tapping a button which gets displayed on the screen.
    If player is successful, then the ragdoll should be disabled and controls enabled again.
    The problem is, after enabling ragdoll mosquito's capsule collider stays in the place where player hit the obstacle, so every time mosquito is revived it hits the obstacle again and ragdoll is being enabled again and so on.
    At the moment, my solution is to instantiate another mosquito once player has succeeded with revival, but it's also causing some issues, for example if I hit the obstacle for the second time, it does not allow me to control it again and its behavior is getting weird.
    I have an idea to make three separate mosquito prefabs (3 is the number of times player is able to revive the mosquito before game over) with separate variants of the same scripts... But I have a feeling that this is not really good approach.
    Does anyone have any ideas what could be done to handle it the most effectively?
     
  2. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    Maybe you can disable physics for your colliders for 3-4 seconds and push up your mosquito into the collider position.
    I didnt played with ragdoll so, my answer might not be the best.

    Or just reload your scene but first saving the last position of the mosquito and place the mosquito after scene loaded at saved position. Disable physics for 1-2-3 seconds and done!
    Also if you have random encounters, walls,etc.. you need to save them all and place it into positions after reload.
    PlayerPrefabs will do the job for you very well.
     
  3. marekkula

    marekkula

    Joined:
    Oct 11, 2016
    Posts:
    22
    Thank you for reply. I am afraid loading the scene again is not an option, the revive must happen immediately as I will need to play specific animation on my mosquito during his revival.
    I didn;t thought about disabling physics for collider. How can I achieve this?
     
  4. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
  5. marekkula

    marekkula

    Joined:
    Oct 11, 2016
    Posts:
    22
    Hi,
    I've managed to implement the revive feature, it's working nice. I have followed the approach with multiple mosquito prefabs. It's working nice, so I will leave it like this.

    I have another question though.

    How can I make my mosquito face the opposite direction he was facing while hitting the obstacle? I've tried in many ways but I've failed...

    Currently, after revive I'm setting random Y rotation, but this is not a perfect solution... I just want to set Y rotation opposite to the mosquito rotation at the time of hitting the obstacle. Any ideas?
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Code (CSharp):
    1. transform.rotation = Quaternion.LookRotation(-transform.forward, transform.up);
     
  7. marekkula

    marekkula

    Joined:
    Oct 11, 2016
    Posts:
    22
    Thank you, but this is not working :(
     
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I think the problem is with your structure.

    what I think is happening is the ragdoll is a child of the main object and when activated the main object is staying in place and the ragdoll is moving both in world space and local space(because the parent isn't moving) generating an offset between the two.

    if this is the case I see 2 options:

    1) when the player revives - set the main object position to whatever it is plus the ragdoll local position(moving it to where the ragdoll is) and moving the ragdoll back to be at (0,0,0) local to keep him in place

    2) keep them as separate objects and snap them each other when you swap
     
  9. marekkula

    marekkula

    Joined:
    Oct 11, 2016
    Posts:
    22
    I actually got this to work. It was a bit tricky, but now it's OK.

    The problem was in my controller script which was setting the player rotation to the same values every time new mosquito was instantiated.

    As I was only interested in changing Y rotation, I have used this piece of code:
    Quaternion.LookRotation(-transform.forward, transform.up).y * some weird calculated number as I've noticed the Y rotation I was getting via script was always about 0.7434553.