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()
{
}
}
Thanks! With a little bug fixing on this script it'll be perfect!
– wyndonelbryan