Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved Pick Up Objects With New Input System

Discussion in 'Input System' started by tysongrunter, Mar 25, 2023.

  1. tysongrunter

    tysongrunter

    Joined:
    Mar 25, 2023
    Posts:
    19
    Okay, I am working on a 3D video game. I am using the new input system, and I am working on context-sensitive controls. The idea is that when you press C, the player will pick up an object with the Grabbable tag, and be able to carry them around. Press C again to drop the object, and press X (which is used for attacking when empty-handed) to throw the object. Here is my code to do so with C# (warning: long):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     //holds the rigidbody
    9.     [SerializeField]
    10.     Rigidbody rb;
    11.     //holds Kaitlyn's max speed while walking
    12.     [SerializeField]
    13.     float WalkSpeed;
    14.     //holds the direction Kaitlyn moves in
    15.     private Vector3 forceDirection = Vector3.zero;
    16.     //holds Kaitlyn's terminal velocity
    17.     [SerializeField]
    18.     float terminalVelocity;
    19.     //holds the player's controls
    20.     [SerializeField]
    21.     private PlayerControls controls;
    22.     //holds the movement
    23.     [SerializeField]
    24.     private InputAction move;
    25.     //holds the force Kaitlyn applies to herself while moving
    26.     [SerializeField]
    27.     private float movementForce;
    28.     //holds the camera
    29.     [SerializeField]
    30.     private Camera playerCamera;
    31.     //holds Kaitlyn's jump force
    32.     [SerializeField]
    33.     float JumpForce;
    34.     //holds the player's capsule collider
    35.     [SerializeField]
    36.     private CapsuleCollider capsule;
    37.     //holds the player's attack strength
    38.     public int strength;
    39.     //accesses the transform that Kaitlyn gives to the items she grabs
    40.     [SerializeField]
    41.     public Transform holdSpace;
    42.     //checks if Kaitlyn is not holding something
    43.     [SerializeField]
    44.     private bool emptyHand;
    45.     //stores the rigidbody of the object picked up
    46.     [SerializeField]
    47.     Rigidbody _heldObject;
    48.     //holds the spring force of throwable objects
    49.     [SerializeField]
    50.     float springForce;
    51.     //holds the damping force of throwable objects
    52.     [SerializeField]
    53.     float dampingForce;
    54.  
    55.     //gets Kaitlyn's rigidbody, collider, and controls
    56.     void Awake()
    57.     {
    58.         rb = GetComponent<Rigidbody>();
    59.         controls = new PlayerControls();
    60.         capsule = GetComponent<CapsuleCollider>();
    61.         emptyHand = true;
    62.     }
    63.  
    64.     //enables Kaitlyn's moveset
    65.     private void OnEnable()
    66.     {
    67.         controls.Kaitlyn.Jump.started += DoJump;
    68.         controls.Kaitlyn.Crouch.started += DoCrouch;
    69.         controls.Kaitlyn.Crouch.canceled += DoStand;
    70.         controls.Kaitlyn.Sprint.started += DoSprint;
    71.         controls.Kaitlyn.Sprint.canceled += DoStand;
    72.         controls.Kaitlyn.Attack.started += DoAttack;
    73.         controls.Kaitlyn.Grab.started += DoGrab;
    74.         move = controls.Kaitlyn.Move;
    75.         controls.Kaitlyn.Enable();
    76.     }
    77.  
    78.     //disables Kaitlyn's moveset
    79.     private void OnDisable()
    80.     {
    81.         controls.Kaitlyn.Jump.started -= DoJump;
    82.         controls.Kaitlyn.Crouch.started -= DoCrouch;
    83.         controls.Kaitlyn.Crouch.canceled -= DoStand;
    84.         controls.Kaitlyn.Sprint.started -= DoSprint;
    85.         controls.Kaitlyn.Sprint.canceled -= DoStand;
    86.         controls.Kaitlyn.Attack.started -= DoAttack;
    87.         controls.Kaitlyn.Grab.started -= DoGrab;
    88.         controls.Kaitlyn.Disable();
    89.     }
    90.  
    91.     private void FixedUpdate()
    92.     {
    93.         //holds Kaitlyn's movement
    94.         forceDirection += move.ReadValue<Vector2>().x * GetCameraRight(playerCamera) * movementForce;
    95.         forceDirection += move.ReadValue<Vector2>().y * GetCameraForward(playerCamera) * movementForce;
    96.         //applies force to Kaitlyn
    97.         rb.AddForce(forceDirection, ForceMode.Impulse);
    98.         forceDirection = Vector3.zero;
    99.         //lets Kaitlyn fall with gravity
    100.         if(rb.velocity.y < 0f)
    101.         {
    102.             rb.velocity -= Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;
    103.         }
    104.         //holds Kaitlyn's horizontal velocity
    105.         Vector3 horizontalVelocity = rb.velocity;
    106.         horizontalVelocity.y = 0;
    107.         //stops Kaitlyn from moving faster than her max speed
    108.         if(horizontalVelocity.sqrMagnitude > WalkSpeed * WalkSpeed)
    109.         {
    110.             rb.velocity = horizontalVelocity.normalized * WalkSpeed + Vector3.up * rb.velocity.y;
    111.         }
    112.         //calls the LookAt() function
    113.         LookAt();
    114.     }
    115.  
    116.     //bases Kaitlyn's X-axis off the camera's
    117.     private Vector3 GetCameraRight(Camera camera)
    118.     {
    119.         Vector3 right = camera.transform.right;
    120.         right.y = 0;
    121.         return right.normalized;
    122.     }
    123.  
    124.     //bases Kaitlyn's Z-axis off the camera's
    125.     private Vector3 GetCameraForward(Camera camera)
    126.     {
    127.         Vector3 forward = camera.transform.forward;
    128.         forward.y = 0;
    129.         return forward.normalized;
    130.     }
    131.  
    132.     //makes Kaitlyn turn in the direction she looks
    133.     private void LookAt()
    134.     {
    135.         Vector3 direction = rb.velocity;
    136.         direction.y = 0f;
    137.         if(move.ReadValue<Vector2>().sqrMagnitude > 0.1f && direction.sqrMagnitude > 0.1f)
    138.         {
    139.             rb.rotation = Quaternion.LookRotation(direction, Vector3.up);
    140.         }
    141.         else
    142.         {
    143.             rb.angularVelocity = Vector3.zero;
    144.         }
    145.     }
    146.  
    147.     //makes Kaitlyn jump, if she is touching the ground
    148.     private void DoJump(InputAction.CallbackContext obj)
    149.     {
    150.         //only lets Kaitlyn jump if she's in contact with the ground
    151.         if (IsGrounded())
    152.         {
    153.             forceDirection += Vector3.up * JumpForce;
    154.         }
    155.     }
    156.  
    157.     //checks if Kaitlyn is standing on solid ground
    158.     private bool IsGrounded()
    159.     {
    160.         //contains the ground check raycast's origin and direction
    161.         Ray ray = new Ray(this.transform.position + Vector3.up, Vector3.down);
    162.         //sets to true if the raycast hits something
    163.         if (Physics.Raycast(ray, out RaycastHit hit, 3f))
    164.         {
    165.             return true;
    166.         }
    167.         //sets to false if it doesn't hit
    168.         else
    169.         {
    170.             return false;
    171.         }
    172.     }
    173.  
    174.     //lets Kaitlyn crouch, reducing her height and top speed
    175.     private void DoCrouch(InputAction.CallbackContext obj)
    176.     {
    177.         capsule.height = 1.5f;
    178.         WalkSpeed = 1f;
    179.         movementForce = 1f;
    180.     }
    181.  
    182.     //lets Kaitlyn stand up straight after crouching or sprinting, resetting her height, top speed, and acceleration
    183.     private void DoStand(InputAction.CallbackContext obj)
    184.     {
    185.         capsule.height = 3f;
    186.         WalkSpeed = 2f;
    187.         movementForce = 1f;
    188.     }
    189.  
    190.     //allows Kaitlyn to run, increasing her top speed and acceleration
    191.     private void DoSprint(InputAction.CallbackContext obj)
    192.     {
    193.         WalkSpeed = 10f;
    194.         movementForce = 2.5f;
    195.     }
    196.  
    197.     //lets Kaitlyn attack
    198.     private void DoAttack(InputAction.CallbackContext obj)
    199.     {
    200.         //sets the origin at the player's position and the direction at in front of Kaitlyn
    201.         Ray ray = new Ray(this.transform.position, this.transform.forward);
    202.         //checks if there's something 1.665 m in front of Kaitlyn
    203.         if (Physics.Raycast(ray, out RaycastHit hit, 1.665f))
    204.         {
    205.             //holds the rigidbody of the grabbable object
    206.             Rigidbody otherRB = hit.rigidbody;
    207.             //gets the rigidbody of the grabbable object
    208.             //otherRB = hit.collider.gameObject.GetComponent<Rigidbody>();
    209.             //checks if that thing is tagged as damageable, and if so, communicates to the damageable script
    210.             if (hit.transform.gameObject.tag == "Damageable" && emptyHand == true)
    211.             {
    212.                 hit.transform.gameObject.SendMessage("TakeDamage", strength);
    213.             }
    214.             //checks if Kaitlyn is holding something
    215.             else if (_heldObject != null)
    216.             {
    217.                 hit.transform.parent = null;
    218.                 otherRB.useGravity = true;
    219.                 emptyHand = true;
    220.                 Vector3 targetDelta = holdSpace.position - _heldObject.position;
    221.                 _heldObject.velocity += (targetDelta * springForce) - (_heldObject.velocity * Time.deltaTime * dampingForce);
    222.             }
    223.         }
    224.     }
    225.  
    226.     //lets Kaitlyn grab objects
    227.     private void DoGrab(InputAction.CallbackContext obj)
    228.     {
    229.         //sets the origin at the player's position and the direction at in front of Kaitlyn
    230.         Ray ray = new Ray(this.transform.position, this.transform.forward);
    231.         //checks if there's something 1.665 m in front of Kaitlyn
    232.         if (Physics.Raycast(ray, out RaycastHit hit, 1.665f))
    233.         {
    234.             //holds the rigidbody of the grabbable object
    235.             Rigidbody otherRB = hit.rigidbody;
    236.             //gets the rigidbody of the grabbable object
    237.             //otherRB = hit.collider.gameObject.GetComponent<Rigidbody>();
    238.             //checks if the grabbable object has an rigidbody
    239.             if(otherRB != null)
    240.             {
    241.                 _heldObject = _heldObject == otherRB ? null : otherRB;
    242.             }
    243.             //checks if that thing is tagged as grabbable, and if Kaitlyn is holding something
    244.             if (hit.transform.gameObject.tag == "Grabbable" && emptyHand == true)
    245.             {
    246.                 hit.transform.SetPositionAndRotation(holdSpace.transform.position, holdSpace.transform.rotation);
    247.                 hit.transform.parent = holdSpace.transform;
    248.                 otherRB.useGravity = false;
    249.                 emptyHand = false;
    250.             }
    251.             //checks if Kaitlyn is holding something
    252.             else if (_heldObject != null)
    253.             {
    254.                 hit.transform.parent = null;
    255.                 otherRB.useGravity = true;
    256.                 emptyHand = true;
    257.             }
    258.         }
    259.     }
    260. }
    261.  
    Anyways, while it can pick up the object I put in my test scene, if I set to turn useGravity off, the object drifts away from the player, and doesn't move with them. And if I were to set it to turn IsKinematic on, it disables the player's movement. What changes to the code would I need, if I were to want the player to be able to pick up, carry, drop, and throw certain objects?
     
  2. tv_Never

    tv_Never

    Joined:
    Nov 11, 2021
    Posts:
    11
    Would this be useful?


    You say the object drifts when picked up. This is because it has velocity. You could just set the velocity to 0 on the object while picked up.
     
    tysongrunter likes this.
  3. tysongrunter

    tysongrunter

    Joined:
    Mar 25, 2023
    Posts:
    19
    Hopefully.

    UPDATE: It works. The player can pick things up, carry them around, and drop them. However, thrown objects are thrown at a diagonal. How do I get it so that the player can throw objects straight ahead? I've been able to implement a parabola.
     
    Last edited: Mar 25, 2023
  4. tv_Never

    tv_Never

    Joined:
    Nov 11, 2021
    Posts:
    11
    I've edited my comment, look at it again
     
  5. tysongrunter

    tysongrunter

    Joined:
    Mar 25, 2023
    Posts:
    19
    I did set velocity and angular velocity to zero upon being grabbed, but it still goes diagonally. What am I doing wrong? Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Grabbable : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private Rigidbody rb;
    9.     [SerializeField]
    10.     private Transform holdSpace;
    11.     [SerializeField]
    12.     private float horizontalForce;
    13.     [SerializeField]
    14.     private float verticalForce;
    15.     [SerializeField]
    16.     private float sidewaysForce;
    17.     [SerializeField]
    18.     private Transform player;
    19.  
    20.     private void Awake()
    21.     {
    22.         rb = GetComponent<Rigidbody>();
    23.     }
    24.  
    25.     public void Grab(Transform holdSpace)
    26.     {
    27.         this.holdSpace = holdSpace;
    28.         rb.useGravity = false;
    29.         rb.velocity = Vector3.zero;
    30.         rb.angularVelocity = Vector3.zero;
    31.     }
    32.  
    33.     private void FixedUpdate()
    34.     {
    35.         if(holdSpace != null)
    36.         {
    37.             float lerpSpeed = 10f;
    38.             Vector3 newPosition = Vector3.Lerp(transform.position, holdSpace.position, Time.deltaTime * lerpSpeed);
    39.             rb.MovePosition(newPosition);
    40.         }
    41.     }
    42.  
    43.     public void Drop()
    44.     {
    45.         this.holdSpace = null;
    46.         rb.useGravity = true;
    47.     }
    48.  
    49.     public void Throw()
    50.     {
    51.         this.holdSpace = null;
    52.         rb.useGravity = true;
    53.         Vector3 forceDirection = player.transform.forward;
    54.         Vector3 sidewaysDirection = player.transform.right;
    55.         RaycastHit hit;
    56.         if(Physics.Raycast(player.position, player.forward, out hit, 100f))
    57.         {
    58.             forceDirection = (hit.point).normalized;
    59.         }
    60.         Vector3 ApplyForce = forceDirection * horizontalForce + transform.up * verticalForce + sidewaysDirection * sidewaysForce;
    61.         rb.AddForce(ApplyForce, ForceMode.Impulse);
    62.     }
    63. }
    64.  
    What changes need to be placed so thrown objects go straight ahead of the player?
     
  6. tysongrunter

    tysongrunter

    Joined:
    Mar 25, 2023
    Posts:
    19
    SOLUTION: I managed to make it work. Here's the Player script I'm using:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     //holds the rigidbody
    9.     [SerializeField]
    10.     Rigidbody rb;
    11.     //holds Kaitlyn's max speed while walking
    12.     [SerializeField]
    13.     float WalkSpeed;
    14.     //holds the direction Kaitlyn moves in
    15.     private Vector3 forceDirection = Vector3.zero;
    16.     //holds Kaitlyn's terminal velocity
    17.     [SerializeField]
    18.     float terminalVelocity;
    19.     //holds the player's controls
    20.     [SerializeField]
    21.     private PlayerControls controls;
    22.     //holds the movement
    23.     [SerializeField]
    24.     private InputAction move;
    25.     //holds the force Kaitlyn applies to herself while moving
    26.     [SerializeField]
    27.     private float movementForce;
    28.     //holds the camera
    29.     [SerializeField]
    30.     private Camera playerCamera;
    31.     //holds Kaitlyn's jump force
    32.     [SerializeField]
    33.     float JumpForce;
    34.     //holds the player's capsule collider
    35.     [SerializeField]
    36.     private CapsuleCollider capsule;
    37.     //holds the player's attack strength
    38.     public int strength;
    39.     //accesses the transform that Kaitlyn gives to the items she grabs
    40.     public Transform holdSpace;
    41.     //stores the rigidbody of the object picked up
    42.     [SerializeField]
    43.     Rigidbody _heldObject;
    44.     //holds the spring force of throwable objects
    45.     [SerializeField]
    46.     float springForce;
    47.     //holds the damping force of throwable objects
    48.     [SerializeField]
    49.     float dampingForce;
    50.     [SerializeField]
    51.     private Grabbable grabbable;
    52.  
    53.     //gets Kaitlyn's rigidbody, collider, and controls
    54.     void Awake()
    55.     {
    56.         rb = GetComponent<Rigidbody>();
    57.         controls = new PlayerControls();
    58.         capsule = GetComponent<CapsuleCollider>();
    59.     }
    60.  
    61.     //enables Kaitlyn's moveset
    62.     private void OnEnable()
    63.     {
    64.         controls.Kaitlyn.Jump.started += DoJump;
    65.         controls.Kaitlyn.Crouch.started += DoCrouch;
    66.         controls.Kaitlyn.Crouch.canceled += DoStand;
    67.         controls.Kaitlyn.Sprint.started += DoSprint;
    68.         controls.Kaitlyn.Sprint.canceled += DoStand;
    69.         controls.Kaitlyn.Attack.started += DoAttack;
    70.         controls.Kaitlyn.Grab.started += DoGrab;
    71.         move = controls.Kaitlyn.Move;
    72.         controls.Kaitlyn.Enable();
    73.     }
    74.  
    75.     //disables Kaitlyn's moveset
    76.     private void OnDisable()
    77.     {
    78.         controls.Kaitlyn.Jump.started -= DoJump;
    79.         controls.Kaitlyn.Crouch.started -= DoCrouch;
    80.         controls.Kaitlyn.Crouch.canceled -= DoStand;
    81.         controls.Kaitlyn.Sprint.started -= DoSprint;
    82.         controls.Kaitlyn.Sprint.canceled -= DoStand;
    83.         controls.Kaitlyn.Attack.started -= DoAttack;
    84.         controls.Kaitlyn.Grab.started -= DoGrab;
    85.         controls.Kaitlyn.Disable();
    86.     }
    87.  
    88.     //called once a frame
    89.     private void FixedUpdate()
    90.     {
    91.         //holds Kaitlyn's movement
    92.         forceDirection += move.ReadValue<Vector2>().x * GetCameraRight(playerCamera) * movementForce;
    93.         forceDirection += move.ReadValue<Vector2>().y * GetCameraForward(playerCamera) * movementForce;
    94.         //applies force to Kaitlyn
    95.         rb.AddForce(forceDirection, ForceMode.Impulse);
    96.         forceDirection = Vector3.zero;
    97.         //lets Kaitlyn fall with gravity
    98.         if(rb.velocity.y < 0f)
    99.         {
    100.             rb.velocity -= Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;
    101.         }
    102.         //holds Kaitlyn's horizontal velocity
    103.         Vector3 horizontalVelocity = rb.velocity;
    104.         horizontalVelocity.y = 0;
    105.         //stops Kaitlyn from moving faster than her max speed
    106.         if(horizontalVelocity.sqrMagnitude > WalkSpeed * WalkSpeed)
    107.         {
    108.             rb.velocity = horizontalVelocity.normalized * WalkSpeed + Vector3.up * rb.velocity.y;
    109.         }
    110.         //calls the LookAt() function
    111.         LookAt();
    112.     }
    113.  
    114.     //bases Kaitlyn's X-axis off the camera's
    115.     private Vector3 GetCameraRight(Camera camera)
    116.     {
    117.         Vector3 right = camera.transform.right;
    118.         right.y = 0;
    119.         return right.normalized;
    120.     }
    121.  
    122.     //bases Kaitlyn's Z-axis off the camera's
    123.     private Vector3 GetCameraForward(Camera camera)
    124.     {
    125.         Vector3 forward = camera.transform.forward;
    126.         forward.y = 0;
    127.         return forward.normalized;
    128.     }
    129.  
    130.     //makes Kaitlyn turn in the direction she looks
    131.     private void LookAt()
    132.     {
    133.         Vector3 direction = rb.velocity;
    134.         direction.y = 0f;
    135.         if(move.ReadValue<Vector2>().sqrMagnitude > 0.1f && direction.sqrMagnitude > 0.1f)
    136.         {
    137.             rb.rotation = Quaternion.LookRotation(direction, Vector3.up);
    138.         }
    139.         else
    140.         {
    141.             rb.angularVelocity = Vector3.zero;
    142.         }
    143.     }
    144.  
    145.     //makes Kaitlyn jump, if she is touching the ground
    146.     private void DoJump(InputAction.CallbackContext obj)
    147.     {
    148.         //only lets Kaitlyn jump if she's in contact with the ground
    149.         if (IsGrounded())
    150.         {
    151.             forceDirection += Vector3.up * JumpForce;
    152.         }
    153.     }
    154.  
    155.     //checks if Kaitlyn is standing on solid ground
    156.     private bool IsGrounded()
    157.     {
    158.         //contains the ground check raycast's origin and direction
    159.         Ray ray = new Ray(this.transform.position + Vector3.up, Vector3.down);
    160.         //sets to true if the raycast hits something
    161.         if (Physics.Raycast(ray, out RaycastHit hit, 3f))
    162.         {
    163.             return true;
    164.         }
    165.         //sets to false if it doesn't hit
    166.         else
    167.         {
    168.             return false;
    169.         }
    170.     }
    171.  
    172.     //lets Kaitlyn crouch, reducing her height and top speed
    173.     private void DoCrouch(InputAction.CallbackContext obj)
    174.     {
    175.         capsule.height = 1.5f;
    176.         WalkSpeed = 1f;
    177.         movementForce = 1f;
    178.     }
    179.  
    180.     //lets Kaitlyn stand up straight after crouching or sprinting, resetting her height, top speed, and acceleration
    181.     private void DoStand(InputAction.CallbackContext obj)
    182.     {
    183.         capsule.height = 3f;
    184.         WalkSpeed = 2f;
    185.         movementForce = 1f;
    186.     }
    187.  
    188.     //allows Kaitlyn to run, increasing her top speed and acceleration
    189.     private void DoSprint(InputAction.CallbackContext obj)
    190.     {
    191.         WalkSpeed = 10f;
    192.         movementForce = 2.5f;
    193.     }
    194.  
    195.     //lets Kaitlyn attack and throw
    196.     private void DoAttack(InputAction.CallbackContext obj)
    197.     {
    198.         //checks if Kaitlyn's hands are empty
    199.         if(grabbable == null)
    200.         {
    201.             //sets the origin at the player's position and the direction at in front of Kaitlyn
    202.             Ray ray = new Ray(this.transform.position, this.transform.forward);
    203.             //checks if there's something 1.665 m in front of Kaitlyn
    204.             if (Physics.Raycast(ray, out RaycastHit hit, 1.665f))
    205.             {
    206.                 //holds the rigidbody of the grabbable object
    207.                 Rigidbody otherRB = hit.rigidbody;
    208.                 //checks if that thing is tagged as damageable, and if so, communicates to the damageable script
    209.                 if (hit.transform.gameObject.tag == "Damageable")
    210.                 {
    211.                     hit.transform.gameObject.SendMessage("TakeDamage", strength);
    212.                 }
    213.             }
    214.         }
    215.         //throws if Kaitlyn is holding something
    216.         else
    217.         {
    218.             grabbable.Throw();
    219.         }
    220.     }
    221.  
    222.     //lets Kaitlyn grab, carry, and drop objects
    223.     private void DoGrab(InputAction.CallbackContext obj)
    224.     {
    225.         //checks if Kaitlyn's holding something
    226.         if(grabbable == null)
    227.         {
    228.             //sets the origin at the player's position and the direction at in front of Kaitlyn
    229.             Ray ray = new Ray(this.transform.position, this.transform.forward);
    230.             //checks if there's something 1.665 m in front of Kaitlyn
    231.             if (Physics.Raycast(ray, out RaycastHit hit, 1.665f))
    232.             {
    233.                 //grabs if that object in front of Kaitlyn has a grabbable script
    234.                 if(hit.transform.TryGetComponent(out grabbable))
    235.                 {
    236.                     grabbable.Grab(holdSpace);
    237.                 }
    238.             }
    239.         }
    240.         //if Kaitlyn is holding something, she drops it
    241.         else
    242.         {
    243.             grabbable.Drop();
    244.             grabbable = null;
    245.         }
    246.     }
    247. }
    248.  
    And here's the grabbable script I'm using:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Grabbable : MonoBehaviour
    6. {
    7.     //holds the grabbable object's rigidbody
    8.     [SerializeField]
    9.     private Rigidbody rb;
    10.     //gets Kaitlyn's hold space
    11.     [SerializeField]
    12.     private Transform holdSpace;
    13.     //holds the forwards force of the thrown object
    14.     [SerializeField]
    15.     private float horizontalForce;
    16.     //holds the upwards force of the thrown object
    17.     [SerializeField]
    18.     private float verticalForce;
    19.     //holds the sideways force of the thrown object
    20.     [SerializeField]
    21.     private float sidewaysForce;
    22.     //holds Kaitlyn's transforms
    23.     [SerializeField]
    24.     private Transform player;
    25.  
    26.     //gets the grabbable object's rigidbody
    27.     private void Awake()
    28.     {
    29.         rb = GetComponent<Rigidbody>();
    30.     }
    31.  
    32.     //places the object within the hold space, once grabbed
    33.     public void Grab(Transform holdSpace)
    34.     {
    35.         this.holdSpace = holdSpace;
    36.         rb.useGravity = false;
    37.         rb.velocity = Vector3.zero;
    38.         rb.angularVelocity = Vector3.zero;
    39.     }
    40.  
    41.     //keeps the grabbed object within the hold space
    42.     private void FixedUpdate()
    43.     {
    44.         if(holdSpace != null)
    45.         {
    46.             float lerpSpeed = 10f;
    47.             Vector3 newPosition = Vector3.Lerp(transform.position, holdSpace.position, Time.deltaTime * lerpSpeed);
    48.             this.transform.rotation = holdSpace.rotation;
    49.             rb.MovePosition(newPosition);
    50.         }
    51.     }
    52.  
    53.     //drops the grabbed object by pressing C
    54.     public void Drop()
    55.     {
    56.         this.holdSpace = null;
    57.         rb.useGravity = true;
    58.     }
    59.  
    60.     //throws the grabbed object by pressing X
    61.     public void Throw()
    62.     {
    63.         this.holdSpace = null;
    64.         rb.useGravity = true;
    65.         Vector3 forceDirection = player.transform.forward;
    66.         Vector3 sidewaysDirection = player.transform.right;
    67.         Vector3 ApplyForce = forceDirection * horizontalForce + transform.up * verticalForce + sidewaysDirection * sidewaysForce;
    68.         rb.AddForce(ApplyForce, ForceMode.Impulse);
    69.     }
    70. }
    71.  
     
    tv_Never likes this.