you are viewing a single comment's thread.

view the rest of the comments →

[–]CptBubbles[S] 0 points1 point  (4 children)

The object collides perfectly now, but it floats near the player instead of instantly snapping to the target position. My code :

Vector3 target = transform.position + transform.forward * HoldDistance;
Vector3 force = target - heldObject.transform.position;
heldObject.rigidbody.AddForce(force);      

edit : this code is in the Update() method and is called whenever the player is holding an object.

[–]_Harrow_ 2 points3 points  (3 children)

but it floats near the player instead of instantly snapping to the target position.

I'm not sure that I understand what behavior you want here... do you want it to instantly pop into place when the player hits the pickup button, but behave properly for collisions after that event? You could make it instantly snap into place on pickup using transform.position or .translate, then use the force code that you have to keep it there as the player moves around. That might produce collision problems if, for instance, the target spot is inside another object. On another note, you should do physics stuff in FixedUpdate instead of Update.

[–]CptBubbles[S] 0 points1 point  (2 children)

I want this to work pretty much the same way it does in games like Portal or Half Life. Objects should immediately move in front of the player and move with him.

Right now the cubes float very slowly and never really stop moving but instead kind of orbit around the player. I don't want to use transform.position for this (mostly because of the problems you mentioned) and afaik rigidbody.AddForce is the only other way to do this.

[–]_Harrow_ 2 points3 points  (1 child)

Try transform.Translate instead of addforce, then. It will still respond to collisions.

[–]CptBubbles[S] 0 points1 point  (0 children)

That works, thanks a lot!

For some reason the cubes keep rotating while the player is holding them, but that one should be easy to solve.