How to disable gravity on an object hit with a raycast?

Hi! I’m trying to make a grabbing system for my game and quite frankly it’s been a pain, in order for my object to not continuously build up velocity I need to disable to gravity on it. btw any other help on the grabbing system would be greatly appreciated!
the script is on my camera’s parent empty

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GrabScript : MonoBehaviour
{
    public GameObject grabPoint;
    public LayerMask isGrabbable;
    public bool isGrabbed;
    public bool isInHand;

    [SerializeField]
    private RaycastHit thingInRange;
    public bool EUREKA;
    public bool isInRange;
    public Rigidbody thingRB;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            if(isGrabbed == true && EUREKA == true && isInRange == false)
            {
                isGrabbed = false;
                EUREKA = false;
                
            } 
        }
        


        Grab();

        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hitInfo;

        if(Physics.Raycast(ray, out hitInfo, 5, isGrabbable))
        {
            isInRange = true;
            thingInRange = hitInfo;

            Debug.DrawLine(ray.origin, hitInfo.point, Color.green);
            if(Input.GetButtonDown("Fire1"))
            {


                if(isGrabbed == false)
                {
                    isGrabbed = true;
                    EUREKA = true;
                }

               
            }
        }
        else
        {
            Debug.DrawLine(ray.origin, ray.origin + ray.direction * 5, Color.red);
            isInRange = false;
        }

        thingRB = thingInRange.GetComponent<Rigidbody>();




    }

    void Grab()
    {
        if(isGrabbed == true)
        {
            thingInRange.transform.position = grabPoint.transform.position;
            thingInRange.transform.rotation = grabPoint.transform.rotation;
            thingRB.useGravity = false;
        }

        else if(isGrabbed == false)
        {
            thingRB.useGravity = true;
        }
    }

    void Throw()
    {
        
    }





},Hi! So I'm trying to make a grab system (it's very messy right now) and I'm wondering, how can I disable gravity on an object I hit with a raycast? Here's my code, also any other help with my grab system's code would be appreciated (its on my camera's parent empty)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GrabScript : MonoBehaviour
{
    public GameObject grabPoint;
    public LayerMask isGrabbable;
    public bool isGrabbed;
    public bool isInHand;

    [SerializeField]
    private RaycastHit thingInRange;
    public bool EUREKA;
    public bool isInRange;
    public Rigidbody thingRB;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            if(isGrabbed == true && EUREKA == true && isInRange == false)
            {
                isGrabbed = false;
                EUREKA = false;
                
            } 
        }
        


        Grab();

        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hitInfo;

        if(Physics.Raycast(ray, out hitInfo, 5, isGrabbable))
        {
            isInRange = true;
            thingInRange = hitInfo;

            Debug.DrawLine(ray.origin, hitInfo.point, Color.green);
            if(Input.GetButtonDown("Fire1"))
            {


                if(isGrabbed == false)
                {
                    isGrabbed = true;
                    EUREKA = true;
                }

               
            }
        }
        else
        {
            Debug.DrawLine(ray.origin, ray.origin + ray.direction * 5, Color.red);
            isInRange = false;
        }

        thingRB = thingInRange.GetComponent<Rigidbody>();




    }

    void Grab()
    {
        if(isGrabbed == true)
        {
            thingInRange.transform.position = grabPoint.transform.position;
            thingInRange.transform.rotation = grabPoint.transform.rotation;
            thingRB.useGravity = false;
        }

        else if(isGrabbed == false)
        {
            thingRB.useGravity = true;
        }
    }

    void Throw()
    {
        
    }





}

2 Answers

2

Suggested rework of your script. I’ve quickly tested it with a very minimal setup on my end, it seems to work fine.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GrabScript : MonoBehaviour
{
    private const string GrabDropButton = "Fire1";
    public GameObject grabPoint;
    public LayerMask isGrabbable;
    private Rigidbody grabbedObject;

    void Update()
    {
        if(grabbedObject == null)
            Detect();
        else
            Carry();
    }

    void Detect()
    {
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hitInfo;

        // Check if we are detecting something with a Rigidbody
        if(Physics.Raycast(ray, out hitInfo, maxDistance: 5, isGrabbable) && hitInfo.collider.TryGetComponent(out Rigidbody rigidbody))
        {
            Debug.DrawLine(ray.origin, hitInfo.point, Color.green);
            if(Input.GetButtonDown(GrabDropButton))
            {
                Grab(rigidbody); // Grab the hit object
            }
        }
        else
        {
            Debug.DrawLine(ray.origin, ray.origin + ray.direction * 5, Color.red);
        }
    }

    void Grab(Rigidbody target)
    {
        grabbedObject = target;
        grabbedObject.transform.SetParent(grabPoint.transform);
        grabbedObject.transform.SetPositionAndRotation(grabPoint.transform.position, grabPoint.transform.rotation);
        grabbedObject.useGravity = false;
    }

    void Carry()
    {
        if(Input.GetButtonDown(GrabDropButton))
        {
            Drop();
        }
    }

    void Drop()
    {
        grabbedObject.transform.SetParent(null);
        grabbedObject.useGravity = true;
        grabbedObject = null;
    }
}

Thanks! With a little bug fixing on this script it'll be perfect!

if(Physics.Raycast(ray, out hitInfo, maxDistance: 5, isGrabbable)
{
hitinfo.point.transform.getcomponent().UseGravity = false
}

sorry because i am not in vscode and i dont access the code.if my code isn’t exist try this code with Capital words