- Home /
Carrying object with MovePosition shaking/vibrate
Hello,I have the possibilty to pick up object, carry them and drop them. That works but while I carrying my object it's shaking. I am using MovePosition in FixedUpdate() I tried several thing but I can't fix it. Have you an idea how to resolve it ?
thanks !
here some part on my code :
Update() :
void Update () {
if(carrying) {
// the desired position on our carrying object
carryPosition = mainCamera.transform.position + mainCamera.transform.forward * distance;
// to avoid possibility the carrying object through the ground
if (carryPosition.y <= distY) {
carryPosition.y = distY;
}
carriedObject.GetComponent<Rigidbody> ().velocity = Vector3.zero;
carriedObject.GetComponent<Rigidbody> ().angularVelocity =Vector3.zero;
// if click on mouse, we drop the carrying object
checkDrop();
// if the object enter in collision with a wall we drop the object
checkCollision ();
} else {
// pick up the object front of the camera
pickup();
}
}
FixedUpdate():
void FixedUpdate(){
if (carrying) {
//Move our carrying object to the carryPosition
carriedObject.GetComponent<Rigidbody> ().MovePosition (carryPosition);
}
}
PickUp() :
void pickup() {
if(Input.GetMouseButtonDown(1)) {
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
// all object which can be pick up have Pickupable.cs as script
Pickupable p = hit.collider.GetComponent<Pickupable>();
pick = p;
// if there is an object pickupable and the distance between the camera and the object is less than distance
if(p!=null && Vector3.Distance(p.gameObject.transform.position, mainCamera.transform.position) < distance) {
carrying = true;
carriedObject = p.gameObject;
p.gameObject.GetComponent<Rigidbody>().useGravity = false;
// rotate the object to the defaut rotation
p.transform.rotation = Quaternion.identity;
float YOrigin = p.GetComponent<Collider> ().bounds.center.y;
float YMin = p.GetComponent<Collider> ().bounds.min.y;
// the distance from the center of the object to the bottom of the object
distY = YOrigin - YMin;
}
}
}
}
Answer by Bilelmnasser · Sep 29, 2017 at 02:56 PM
hi, Make your carried Object a child Of Your Carrying Object.
use Transform.SetParent , than it will move along her parent (remember to set her local position / rotation Once the object parent is set ), no need to extra script setting the position every frame
and to drop it just detach it from her parent :)
Answer by Litleck · Sep 29, 2017 at 05:07 PM
You could use mathf.lerp or matf.smoothdamp. Not sure if this will help much but if you have any questions let me know.
Thanks for the answer, I tried it's better with Lerp but still shaking, set the carriedObject a child of the player is working well
Answer by conceptfac · Oct 17, 2020 at 10:57 PM
Here the same happens! Even localRotation or not! I'm trying to rotate objec in X and Y axis, but only -Y, +Y and -X is working when i rotate +X it shakes
Your answer
Follow this Question
Related Questions
How to give smooth constant move to a player object 1 Answer
Rigidbody Position Changes don't work when moving. 0 Answers
How do I stop my objects from going through walls when I pick them up and drop them down? 2 Answers
Combat begins when colliding with the enemy 1 Answer
Unity5 Enlighten Shadows 1 Answer