Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

add force at position problem

Discussion in 'Scripting' started by Dino1997, Sep 27, 2011.

  1. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    hello ,im making a FPS survival game and i aded
    Code (csharp):
    1.  
    2. if(Physics.Raycast(camRay.origin + camRay.direction * 0.1, camRay.direction, hit, fireRange - hit.distance, hitLayer)) {
    3.               if(hit.rigidbody) {
    4.     hit.rigidbody.AddForceAtPosition(hit.point, transform.forward);
    5.                 }
    6.             }
    7.  
    and im having a problem,when i hit object with rigidbody it goes to right side,whereever i stand or whereever im aiming on box,it goes to right side...what is problem?
     
  2. cat_ninja

    cat_ninja

    Joined:
    Jul 14, 2010
    Posts:
    197
    try transform.TransformDirection(0,0,1) instead of transform.forward?
     
  3. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    not working,its still goes to the right. :/
     
  4. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    does anyone know how to fix it?
     
  5. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,864
    you need to specify a direction and a global position, just read the documents there are samples there.

    In your example your direciton is the hit.point and the position to apply it is transform.forward which will not work as how you want it.
     
  6. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    if i understood you ,i should put direction first then,position.i tried to put direction first but then its going nowhere,its not moving.
     
  7. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,086
    the first parameter of AddForceAtPosition() is both the direction and the amount of force, so you have to multiply it by something to make it have more force. Something like:

    Code (csharp):
    1.  
    2. AddForceAtPosition(transform.forward, hit.point);
    3.  
    Would add 1 unit of force (because transform.forward is a directional vector normalized to 1.0) to an object relative to the position of hit.point, in the direction of transform.forward. So try:

    Code (csharp):
    1.  
    2. var force : float = 1000.0;
    3. AddForceAtPosition(transform.forward*force, hit.point);
    4.  
    And tweak the force variable to change the amount of force applied.
     
  8. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    thank you very much,its working :)