Search Unity

Problem using AddForce

Discussion in 'Physics' started by Shadowing, Mar 2, 2019.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I can't figure out what im doing wrong.
    Just trying to add some small push back when a bullet hits a character.
    This shoots my character across the screen.

    wondering if anyone knows what I'm doing wrong

    Vector3.forward is something like 7,0,-7

    Code (csharp):
    1.  
    2.                 collision.rigidbody.AddForce(Vector3.forward * .0001f, ForceMode.VelocityChange);
    3.  
    4.  
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Vector3.forward is 0,0,1
    This is global frame.
    you would need transform forward perhaps?
    But in fact, you want move char, in same direction, as bullet moving direction.
    So technically, should be something like rb_bullet.velocity.normalized * 0.0001f. Or something like that.
     
    SparrowGS likes this.
  3. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Thanks for the reply man.
    This is still pushing my character across the screen.

    Code (csharp):
    1.  
    2.                 collision.rigidbody.AddForce(transform.forward.normalized * .0001f, ForceMode.VelocityChange);
    3.  
    4.  
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Try change VelocityChange to Acceleration
    You may want increase force.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I don't remember me using the VelocityChange mode but I don't think this should be the case, flying across the screen and such.

    Are you calling this and only this one time at the time of fire?
    What's the objects mass?
    How big is the screen from end-to-end?

    For stuff like that I tend to use the Impulse force mode..

    But I'm more curious about why you think that^, it may point to the problem.
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Seems to be more going on here. For starters I have OnTriggerEnter and OncollisionEnter on the same projectile script. This is cause I have a special bullet that uses a trigger instead so the bullet can pass through a enemy and also effect a enemy behind him etc..

    Whats wierd though is. This bullet I'm using right now has IsTrigger set to false bet yet its tripping OnTriggerEnter();
    The character he hits has a Trigger set to true though on a Sphere Collider which is used for character interaction between players.

    Just to make sure I tested to make sure the bullet doesn't have trigger on. Incase something was turning it on somewhere lol.

    Is Trigger is false here. which is on the bullet
    Code (csharp):
    1.  
    2. Debug.Log(other.name+" "+LayerHit+" "+transform.GetComponent<CapsuleCollider>().isTrigger);
    3.  

    Is Trigger is true here. which is on the character.
    Code (csharp):
    1.  
    2. Debug.Log(other.name+" "+LayerHit+" "+other.isTrigger);
    3.  

    According to what I've always known and what the manual says I think.
    OnTriggerEnter is only called on the gameObject the collider with IsTrigger is attached too.

    But this test is telling me its calling OnTriggerEnter on the script attached to the Bullet when only the character has IsTrigger set to true.
     
  7. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I've been editing a character controller i bought from the asset store for a few years now.

    I notice on his OnCollisionEnter() function
    He is checking to see if a trigger is active collision.collider.IsTrigger

    I don't understand why he would be doing that. Its not gonna stop any OnTriggerEnter scripts from running that the bullet hits. And for what I understand a OnCollision can't occur on a collider set to Trigger?

    Code (csharp):
    1.  
    2.         private void OnCollisionEnter(Collision collision)
    3.         {
    4.             // OnCollisionEnter sometimes gets called multiple times in a single frame so explicitly enable and disable the projectile.
    5.             // If the projectile is already disabled then it doesn't need to schedule another activation.
    6.             if (!enabled || collision.collider.isTrigger || !m_DestroyOnCollision) {
    7.                 return;
    8.             }
    9.             Collide(m_Originator, collision.transform, collision.contacts[0].point, collision.contacts[0].normal, m_DestroyOnCollision);
    10.  
    11. }
    12.  
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    This seams does nothing.
     
  9. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Heres my OnTriggerEnter function

    The IsTrigger option on the bullet collider is false but yet this trips.

    Code (csharp):
    1.  
    2.  
    3.         void OnTriggerEnter(Collider other) {
    4.        
    5.             int LayerHit = other.gameObject.layer;
    6.             Debug.Log(other.name+" "+LayerHit+" "+other.isTrigger);
    7.             if(LayerHit != m_Originator.layer && LayerHit >= 9 && LayerHit <= 13 || LayerHit == 31){
    8.            
    9.                 DamagedController m_DamagedController = other.gameObject.GetComponent<DamagedController>();
    10.                
    11.                 if(other.attachedRigidbody){
    12.                     other.attachedRigidbody.AddForce(m_AppliedForce - m_Rigidbody.velocity, ForceMode.VelocityChange);
    13.                 }
    14.                 if(m_DamagedController){
    15.                     m_DamagedController.DamageControl(0, transform.position, new Vector3(), m_Originator,DamageEvent);
    16.                     other.gameObject.GetComponent<Health>().Damage(m_DamageAmount,transform.position,transform.forward);
    17.                 }
    18.             }
    19.         }
    20.  
     
  10. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I'll just do this below for now. so this won't trip anymore. I guess i'll need to make a test project to test this trigger thing out and if it does it there then send it to Unity as a bug.

    so the reason my character was flying across the screen was cause this was being tripped cause I had it addng a force of transform.forward * 800.

    I had a enum variable set for different size forces. Wasn't sure what to set the settings at when I was testing.
    I was over ridding the setting inside OnCollisionEnter not knowing that OnTriggerEnter was being tripped as well.

    Thanks for the help guys.

    Code (csharp):
    1.  
    2.  
    3. void OnTriggerEnter(Collider other) {
    4.    if(other.isTrigger){
    5.            return;
    6.    }
    7.  
    8. }
    9.  
    10.