Search Unity

rigidbody.AddExplosionForce moves the rigidbody instantly if velocity is set from script

Discussion in 'Editor & General Support' started by unity_94d33m, Mar 24, 2019.

  1. unity_94d33m

    unity_94d33m

    Joined:
    Sep 11, 2018
    Posts:
    20
    I'm doing the TANKS tutorial, and I have this line which adds force to it:
    targetRigidbody.AddExplosionForce(m_ExplosionForce, transform.position, m_ExplosionRadius);

    In their video, the tank goes back smoothly, my one is an instant move.

    Edit1 : Figured out something in Movement script is causing it to stop and not slide.

    Edit2 : So I had these two lines to handle the tank's velocity:
    Code (CSharp):
    1.         Vector3 velocity = transform.forward * m_MovementInputValue * m_Speed;
    2.         m_Rigidbody.velocity = velocity;
    Which is why it would stop immediately after explosion, so I had to change it to:
    Code (CSharp):
    1.         Vector3 movement = transform.forward * m_Speed * m_MovementInputValue * Time.deltaTime;
    2.         m_Rigidbody.MovePosition(transform.position + movement);
    However this has the problem that my particle effect(tire effects) that has emission over distance(not time) doesn't work. That only worked if i used velocity code. Is this a bug where explosion effect doesn't work properly for rigidbodies that have velocity set from a script?

    Edit3: Seems like I found the solution from one old thread. Now the tank slides back and the particle effects of tire are working. For this, move the rigidbody using moveposition as shown above. Create another empty game object as a child of the Tank object. Then move the two dust trail objects so that they are a child of the empty gameobject. Add a Rigidbody to the empty gameObject you had created and check "is Kinematic". This allows the dust trails to work and the tank to be pushed back as expected by the exploding shell.
     
    Last edited: Mar 24, 2019