- Home /
objects falling when I use my grab script
I created this grab objects script but when you hold an object it slowly falls down off the screen. does anyone know how I might be able to fix this in the script? public class NewBehaviourScript : MonoBehaviour { public Transform defaultRot; public Transform t; public Transform theDest; public LayerMask yesHit; public bool isHolding; public bool scanner = true; void FixedUpdate() {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Does the ray intersect any objects excluding the player layer
if (scanner = true)
{
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, yesHit))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
t = hit.transform;
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
}
}
if (Input.GetButton("Fire1"))
{
t.position = theDest.position;
isHolding = true;
scanner = false;
}
else
{
isHolding = false;
t = null;
scanner = true;
}
if(isHolding == true)
{
t.position = theDest.position;
t.rotation = defaultRot.rotation;
scanner = false;
}
else
{
scanner = false;
}
}
}
Answer by kot2202 · Jul 08, 2020 at 06:43 PM
The falling object has rigidbody, right? Set kinematic to true so it doesn't use physics,gravity https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html
but will the object being picked up still have gravity?
You have to set it to non kinematic right after you stop grabbing it. You have to do these things in script, not in inspector.