- Home /
Holding objects, How to deal with object clipping
I'm working on a object holding script and have this so far (WASD, mouse click to pickup and drop)
http://kennyist.com/unity/2playergame/foranswers.html
But on this, You can clip objects through walls also it seems generally laggy. How would you go about stopping this from happening.
The script:
public class PickUpObjects : MonoBehaviour {
public Camera camera;
private RaycastHit hit;
bool carry = false;
void FixedUpdate () {
if(Input.GetMouseButtonUp(0)){
if(carry){
carry = false;
hit.rigidbody.useGravity = true;
hit = new RaycastHit();
} else {
if(Input.mousePosition.x < Screen.width / 2){
Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, 100f);
Debug.Log(hit.transform.gameObject.name);
if(Vector3.Distance(transform.position, hit.transform.gameObject.transform.position) < 1){
hit.rigidbody.useGravity = false;
carry = true;
}
}
}
}
if(carry){
Carry();
}
}
void Carry(){
Rigidbody hitRidg = hit.transform.gameObject.rigidbody;
hitRidg.MoveRotation(transform.rotation);
Vector3 newPos = new Vector3(transform.position.x,transform.position.y + 0.2f,transform.position.z);
hitRidg.MovePosition(newPos + transform.forward * 0.2f);
}
}
Answer by s_guy · Nov 20, 2013 at 01:16 AM
Lots of big budget games let weapons and carried objects clip through walls, but I agree it doesn't look great.
One way to do this is to have the walls push the object aside as you move up against them:
Put the object you are holding as a sibling (not a child) to the rigidbody player controller, with a common non-rigidbody parent.
Make the object you are holding a rigidbody if it is not one already.
Make a physics layer that interacts with the environment, but not the player. Put the carried object on this layer.
Attach the carried object to the player by a joint with the desired properties, e.g. a spring joint, or construct a custom (or purchase an) IK apparatus for the arms. Adjust settings such that the carried object tends not to intersect the player in undesirable ways.
...or if you're ok with the player not being able to get as close to walls when carrying something, set the carried object up as a compound collider by making it have a collider and be a child of the player's rigidbody. See "Compound Colliders" at the following location.
http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html
Also, if you want the carried object to just snap / teleport to the carried position, you might try setting its transform directly instead of MovePosition, which may or may not succeed if there is a static obstacle in the way.
Answer by Cherno · Nov 19, 2013 at 09:22 AM
You can use a second camera that only renders the things you are holding, and set this camera's rendering depth to be below the one from your main camera.
Sorry, I meant with collisions. When holding an object you can just push it through the wall, Where as normal collisions work perfectly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122208im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collision detection from specific directions using rigid body character? 0 Answers
Objects with colliders going through walls and each other. 3 Answers
Prevent a specific RB to influence another, but still collide with everything 0 Answers
Force a script generated primitive to test for collision 2 Answers
Moving object with transform.position ignore other objects even if they collided 1 Answer