0

In my unity2D game run in device so player velocity set not zero when collide with ground and I use a FixedUpdate() function for player flap also my player flap on static position.

void FixedUpdate ()
{
    // Jump
    if (isAlive)
    {
        if (didFlap) {
            didFlap = false;
            myRigidbody.velocity = new Vector2(0,bounceSpeed);
            anim.SetTrigger ("Flap");
        }
    }if (myRigidbody.velocity.y >= 0) {
        transform.rotation = Quaternion.Euler (0, 0, 0);
    } else {
        float angle = 0;
        angle = Mathf.Lerp (0,-45,-myRigidbody.velocity.y/7f);
        transform.rotation = Quaternion.Euler (0, 0, angle);
    }
}
void OnCollisionEnter2D(Collision2D target)
{
    if (target.gameObject.tag=="Ground")
    {
         die();
    }
}
void die()
{
    if (isAlive) 
    {
        isAlive  = false;
        anim.Play ("Died");
        myRigidbody.velocity = Vector2.zero;
    }
}
Chetan
  • 11
  • 4
  • So, whats the problem? – Paweł Marecki Jul 20 '16 at 06:10
  • player move on ground when die – Chetan Jul 20 '16 at 06:21
  • Ok but there is no code of movement here and there you should search for problem. In `die()` you just set velocity to [0,0] but than some code will change velocity anyway. I can tell you more if you post part of code that is responsible for movement – Paweł Marecki Jul 20 '16 at 06:30
  • 1
    As already stated, without code we can only guess. Here is mine, adding to what @PawełMarecki said: Are you setting the rigidbody's velocity in the `Update()` method? this is called after the OnCollision functions and would therefore overwrite the velocity of your `die()` function. (see execution order: https://docs.unity3d.com/Manual/ExecutionOrder.html) – nyro_0 Jul 20 '16 at 06:34
  • I try myRigidbody.velocity = new Vector2(0,0) but still player move on ground. – Chetan Jul 20 '16 at 06:36
  • 1
    Show us your `Update` und `FixedUpdate` functions, otherwise we won't be able to help you – nyro_0 Jul 20 '16 at 06:39
  • @ xyLe_ I use FixedUpdate() for player movement. – Chetan Jul 20 '16 at 06:41
  • Please, update your question with this code. Its really hard to read code like this. – Paweł Marecki Jul 20 '16 at 07:00
  • You may try to set `myRigidbody.isKinematic` to `true`. Maybe there is something what make your's object move eg. gravity or bouncines on physics material. – Paweł Marecki Jul 20 '16 at 07:50
  • more of a setup question: have you set your tags correctly -> is `die()` even called on collision? – nyro_0 Jul 20 '16 at 07:56
  • @ xyLe_ yes i define tag proper – Chetan Jul 20 '16 at 08:02

1 Answers1

0

So if I am understanding this right you can still move the player around after they died in the game.

so what you need to do is when the player dies is make a Boolean that is true when the player dies

   void Update()
   {
        if (dead == false)
        {
            //player movement code hear 
        }
   }

this way if you are dead than you can't move because you player control code is not accessible.

I hope this helps.