Search Unity

Add force off in direction

Discussion in 'Physics' started by gaz99, Dec 15, 2014.

  1. gaz99

    gaz99

    Joined:
    May 1, 2014
    Posts:
    9
    Hey,

    I'm currently trying to pass an object between two other objects when i hit space using AddForce. I've got it working but let's say 1/10 of the passes will not go in the right direction.

    Now i debug draw rays which tell me the math is right as the pass that doesn't go in the right direction will still draw the ray pointing to the correct direction but go off in a different direction. This has led me to believe it may be something to do with the physics properties of both the surface and/or the object being passed ?


    The surface is a plane with the default Ice Phyiscis material applied to a box collider. It's rigidbody is set to be kinematic with no gravity and has its position and rotation frozen. All other parameters left default.

    The object being passed has a convex mesh collider with a physics material with the following:
    Dynamic friction = 1
    static friction = 1
    bounciness = 0.3
    both combines set to multiply
    All other parameters left default.

    Its rigidbody uses gravity with its rotation frozen, all other parameters left default.

    Code for passing the object, passStart is the position of the object being passed, passTarget is the object i am passing to (slightly in front)
    Code (CSharp):
    1. public void AttemptPass(GameObject target)
    2.     {
    3.  
    4.         passStart = rigidbody.transform.position;
    5.         passTarget = target.rigidbody.transform.position + (target.rigidbody.transform.TransformDirection(Vector3.forward) * (7*offset));
    6.  
    7.         Vector3 direction = Vector3.Normalize(passTarget - passStart);
    8.         direction = new Vector3(direction.x,0,direction.z);
    9.  
    10.         rigidbody.transform.rotation = Quaternion.LookRotation (direction, transform.up);
    11.  
    12.         rigidbody.AddForce(direction * passForce);
    13.         Debug.DrawRay(rigidbody.transform.position, direction * 2000, Color.red, 3f);
    14.      
    15.     }
    result of a pass gone stray would look like something like this image where v' is the correct direction ray drawn to the target and v is the actual movement of the object, the amount of error is never the same either

    Any help would be apprectiated

    Thanks