- Home /
Grabbed object won't stay still
Happy easter, dear Unitians.
I am making a game where one of the main elements on interaction is grabbing on moving stuff around. I got the to point where I can grab objects fine but only way I found out to keep the objects still (i.e. not floating around uncontrollably) is to make them kinematic. However, this allows them to clip trough walls which is something I don't want.
So, is there any other way to keep Rigidbodies steady on one spot while grabbing them? Here's the part of the script that controls that:
Vector3 ObjectPos;
Quaternion ObjectRot;
bool canPick = true;
bool picking = false;
bool guipick = false;
GameObject pickObj;
GameObject pickref;
void Start () {
pickref = GameObject.FindWithTag ("Pickupref");
pickObj = pickref;
}
public void Update () {
ObjectPos = transform.position;
ObjectRot = transform.rotation;
if (Input.GetMouseButtonDown (0) && canPick) {
picking = true;
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 3) && hit.collider.gameObject.tag == "GenericObject") {
pickObj = hit.collider.gameObject;
hit.rigidbody.useGravity = false;
hit.rigidbody.isKinematic = true;
//hit.collider.isTrigger = true;
hit.transform.parent = gameObject.transform;
transform.position = ObjectPos;
transform.rotation = ObjectRot;
}
While this doesn't exactly hold the object still, it does work so you can have the object in your hands but it bumps into things. Get a lerped value between the current position and a desired position, then use rigidbody.$$anonymous$$ovePosition ins$$anonymous$$d of setting the transform :
calcPos = Vector3.Lerp( currentPos, desiredPos, grabLerpFactor );
rigidObject.$$anonymous$$ovePosition( calcPos );
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Prevent a specific RB to influence another, but still collide with everything 0 Answers
Pick Up Object using Rigidbody FPS 0 Answers
Multiple Cars not working 1 Answer
How to grab objects in 3rd person LITTLE BIG PLANET STYLE 2 Answers