Rigidbody not working

Started by Sirrah025 1 post 119 views

Reply on sbox.game
  1. #1
    Sirrah025
    I've been trying to code a grab component where objects can be picked up on used and carried in a similar manner to REPO. It should reference an empty object child of the camera and apply a force on the rigidbody towards that position. But the cube just remains still. Am I doing anything wrong in applying the force on the rigidbody component? Code below.

    public sealed class Grabbable : Component, Component.IPressable
    {
        private GameObject transformObject;
        private PlayerGrabObject grabObjectData;
        private Rigidbody rigidbody;
        private bool held = false;
        
        // Grabs camera, empty child of camera, and PlayerGrabObject component
        protected override void OnStart()
        {
            CameraComponent camera = Scene.GetAllComponents<CameraComponent>().FirstOrDefault( x => x.IsMainCamera );
            GameObject cameraGameObject = camera.GameObject;
            transformObject = cameraGameObject.Children.FirstOrDefault( c => c.Name == "Hold Position");

            rigidbody = this.GetComponent<Rigidbody>();

            grabObjectData = transformObject.GetComponent<PlayerGrabObject>();
        }

        // Called in OnFixedUpdate while pressed is true
        private void moveToTransform()
        {
            if (WorldPosition == transformObject.WorldPosition) return;
            
            Vector3 direction = getDirection(WorldPosition, transformObject.WorldPosition);
            rigidbody.ApplyImpulse(direction * grabObjectData.objectMoveForce );
        }
    }