I can't addforce because of velocity

I am moving my character with velocity. But (there is a bomb in game) when the bomb bombs it doesn’t addforce to character becasuse of velocity. Here is the movement code:

    float y = Input.GetAxis("Vertical");
    var v3 = -tra.forward * hız*y;
    v3.y = rigi.velocity.y;
    rigi.velocity = v3;
    anim.SetFloat("f", y);

and here is the bomb code:

   public void OnTriggerEnter(Collider other)
{
    if(other.tag=="bumbum")
    {
        Vector3 v1 = transform.position;
        Vector3 v2 = other.gameObject.transform.position;
        Vector3 v3 = v1 - v2;
        rigi.AddForce(v3 * 20, ForceMode.Impulse);
    }
}

How can I fix this?

@Ege10
It’s because AddForce works using rigidbody velocity. In your movement script you are overriding it with your movement vector.

This are solutions that come to my mind:

.1. In movement instead of assigning movement to velocity, multiply current velocity by small number to simulate drag and add movement like that:

    float drag = 0.1f;
    float y = Input.GetAxis("Vertical");
            var v3 = -transform.forward * hız * y;
            rigid.velocity = new Vector3(rigid.velocity.x * drag, rigid.velocity.y, rigid.velocity.z * drag);
            rigid.velocity += v3;

.2. When explosion happens disable movement for some time.

.3. Instead of assigning movement to velocity, add it and than in next update subtract last movement vector multiplied by drag force from velocity before adding the new one.

.4. Make explosion as Coroutine that lasts some time and adds this force to velocity after your movement script

.5. I searched through Internet and found this script that should work with external forces http://wiki.unity3d.com/index.php/RigidbodyFPSWalker

Also make sure to place your movement script in FixedUpdate :wink:

Hi @Ege10

Add a debug.log in if(other.tag==“bumbum”) to check if the code reaches that line of code.
Also, check if you didn’t forget these things:

  1. add a collider to both bomb and character game objects.
  2. tick the trigger checkbox on character’s collider
  3. add a dynamic rigid body to at least one of them.
  4. check if you add a tag of bumbum to the bomb game object.
  5. if the add collider2d component to game objects you have to use OnTriggerEnter2D(Collider2D collider) instead.