Search Unity

Question How to add force to an isKinematic object

Discussion in 'Scripting' started by Polarfist1, Sep 23, 2021.

  1. Polarfist1

    Polarfist1

    Joined:
    Aug 16, 2021
    Posts:
    15
    Im trying to add a backwards force to an isKinematic object, though I have heard that physics do not work with objects with this setting on, though when I disable this setting the detection collision with the raycast to the enemy to actually hit it just breaks after around 8 seconds as it detects the ground instead. Any way to add force to isKinematic object? Here is my code with a gun using a raycast, I want it to push the enemy back a little bit each hit:

    Code (CSharp):
    1.         RaycastHit hit;
    2.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    3.         {
    4.             Debug.Log(hit.transform.name);
    5.  
    6.             Enemy enemy = hit.transform.GetComponent<Enemy>();
    7.             if(enemy != null)
    8.             {
    9.                 enemy.TakeDamage(damage);
    10.                 rigidbody.AddForce(transform.forward * 2000f * Time.deltaTime, ForceMode.VelocityChange);
    11.             }
    12.         }
    13.     }
    14. }
     
    Last edited: Sep 23, 2021
  2. Polarfist1

    Polarfist1

    Joined:
    Aug 16, 2021
    Posts:
    15
    Note: also, it seems with IsKinematic turned off, the code does not work and the enemy is stuck in one place vibration, though if the player moves left or right, the enemy still vibrates and follows to the player's left or right. I think this is because the force is constantly being applied, so if anybody could also help in that regard it would be appreciated
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    You mean a Kinematic body. There are three body types: Dynamic, Kinematic and Static.

    Kinematic bodies do not react to forces including being affected by gravity or contacts, that's the whole point of them.

    You move Kinematic bodies with MovePosition and MoveRotation.