示例#1
0
        public void ApplyForce()
        {
            Ray ray = UnityEngine.Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition);

            float impulse;

            float.TryParse(GameObject.Find("ImpulseInputField").GetComponent <InputField>().text, out impulse);

            Vector3 a = ray.origin + (ray.direction * 1000);

            BulletSharp.Math.Vector3 from = new BulletSharp.Math.Vector3(ray.origin.x, ray.origin.y, ray.origin.z),
                                     to   = new BulletSharp.Math.Vector3(a.x, a.y, a.z);

            ClosestRayResultCallback callback = new ClosestRayResultCallback(ref from, ref to);

            BPhysicsWorld world = BPhysicsWorld.Get();

            world.world.RayTest(from, to, callback);

            BulletSharp.Math.Vector3 point = callback.HitNormalWorld;
            BRigidBody part = (BRigidBody)callback.CollisionObject.UserObject;

            foreach (BRigidBody br in GameObject.Find("Robot").GetComponentsInChildren <BRigidBody>())
            {
                if (part == br)
                {
                    Vector3 closestPoint = br.GetComponent <MeshRenderer>().bounds.ClosestPoint(point.ToUnity());
                    Ray     normalRay    = new Ray(point.ToUnity(), closestPoint - point.ToUnity());

                    part.AddImpulseAtPosition(ray.direction.normalized * impulse, point.ToUnity());
                    // part.AddImpulseAtPosition(normalRay.direction.normalized * impulse, normalRay.origin);
                }
            }
        }