- Home /
Pick Up Rigid Body
I have a script (I know it's a bit messy):
function PickUpObject ()
{
PickedUp = true;
object.parent = Camera.main.transform;
rigidbody.useGravity = false;
}
function Update ()
{
if (PickedUp == true)
{
if (collisionCount == 0) //If collisionCount is 0
{
object.localPosition = Vector3.forward * 2; //Put objects local position to 2 * foraward.
}
if (Input.GetKeyUp ("e")) // If E is let go
{
PickedUp = false; //PickedUp is false
object.parent = null; //Unparent object
rigidbody.useGravity = true; //Enable object's gravity
//rigidbody.isKinematic = false;
}
}
}
private var collisionCount = 0; //collisionCount is 0
function OnCollisionEnter () //When object collides
{
collisionCount++; //+collisionCount
}
function OnCollisionExit () //When object stops colliding
{
collisionCount--; //-collisionCount
}
This script doesn't work very well since the object glitches out when it collides with walls. Then I have this other script(It's not mine btw):
function FixedUpdate() //Every fixed framerate frame
{
if(gravityGunState == GravityGunState.Free) //If gGS = GGS is Free
{
if(Input.GetButton("Fire1")) //If Fire1
{
var hit : RaycastHit; //Get info back
if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask)) //From the position, then forward, using hit, length, in a layer.
{
if(hit.rigidbody) //If it hits a rigidbody
{
rigid = hit.rigidbody; ///Get the rigidbody of the object
gravityGunState = GravityGunState.Catch; //gGS = GGS is Catch
}
}
}
}
else if(gravityGunState == GravityGunState.Catch) //Also, if not above, check if gGS = GGS is Catch
{
rigid.MovePosition(transform.position + transform.forward * holdDistance); //Moves rigid to this position + forward at holdDistance
if(!Input.GetButton("Fire1")) //If fire1 is not on
gravityGunState = GravityGunState.Occupied; //gGS = GGS is Occupied
}
This script seems a bit worse than the previous one since objects always go through colliders. (I know gravity isn't switched off and it reacts like a gravity gun with force(which is not what I want(I can easily change this later though))) Finally, I have this script that is in C#(It's also not mine):
public float mCorrectionForce = 50.0f;
public float mPointDistance = 3.0f;
void FixedUpdate()
{
if(heldObject.contraints != RigidbodyConstraints.FreezeRotation)
heldObject.contraints = RigidbodyConstraints.FreezeRotation;
if(heldObject.useGravity)
heldObject.useGravity = false;
Vector3 targetPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 0));
targetPoint += Camera.main.transform.forward * mPointDistance;
Vector3 force = targetPoint - heldObject.transform.position;
heldObject.rigidbody.velocity = force.normalized * heldObject.rigidbody.velocity.magnitude;
heldObject.rigidbody.AddForce(force * mCorrectionForce);
heldObject.rigidbody.velocity *= Mathf.Min(1.0f, force.magnitude / 2);
}
I do not understand this final script properly though. My question would be that what do you think is best and why. Also, if you can tell me basically what script 3 does then it would be great! I think script 3 works best but I haven't got to use it since to a simple compiler error I will fix. You don't have to give a detailed answer (unlike my question) but any reply would be greatly appreciated! (Sorry for the loooong question!)
Your answer
![](https://koobas.hobune.stream/wayback/20220613132634im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can you pls help me fix this rigidbody collision problem 0 Answers
Push PC down icy slope (along negative gradient) 0 Answers
NavMesh Collision Detection? 0 Answers
Ball falls through the floor... why? 1 Answer
Drag & Drop GameObject with MovePosition shoots other Gameobject away in case of them colliding 1 Answer