- Home /
Picked up object passes through other objects
I want to pick up an object and rotate it. But when i picked it up it passes through other objects. Please help. Here is my code.
var SpawnTo : Transform; //your hand for example, attack an object to your character that you want the position of what you picked up to go to
var Object1 : Transform; //what your picking up, the object that you want to move
var dist :float=1.1; //distance at which you can pick things up
private var isHolding = false;
var speed : int;
var friction : float;
var lerpSpeed : float;
private var xDeg : float;
private var yDeg : float;
private var fromRotation : Quaternion;
private var toRotation : Quaternion;
function Start(){
SpawnTo=GameObject.FindWithTag("Distance").transform;
Object1=transform;
}
function Update () {
if(Input.GetKeyDown(KeyCode.E)){ //if you press 'e'
if(Vector3.Distance(SpawnTo.position, Object1.position) <= dist) //if distance is less than dist variable
{
isHolding = !isHolding; //changes isHolding var from false to true
}
}
if(isHolding == true){
Object1.rigidbody.useGravity = false; //sets gravity to not on so it doesn't just fall to the ground
Object1.parent = SpawnTo; //parents the object
Object1.transform.position = SpawnTo.transform.position; //sets position
Object1.transform.rotation = SpawnTo.transform.rotation; //sets rotation
if(Input.GetMouseButton(0)) {
xDeg += Input.GetAxis("Mouse X") * speed * friction;
yDeg -= Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,0);
SpawnTo.transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
}
}
else{ //if isHolding isn't true
SpawnTo.transform.DetachChildren(); //detach child (object) from hand
Object1.rigidbody.useGravity = false; //add the gravity back on
}
}
Answer by save · Aug 12, 2013 at 01:49 PM
A common solution is to render the object on top of all other objects. This is usually done to all other objects which belongs to the 3d-HUD as well, such as hands and weapons for instance. Either through changing material where the shader is rendered on top or a second camera which only sees such objects. The fastest solution is an additional camera which you parent to the same position where your World Camera is, the trick is that you render it afterwards.
Example of camera depth order:
World Camera
Picked up object Camera
GUI Camera
When you pick up an object, you send the object to [another layer][1] which only the HUD-camera sees. To change layer you use:
gameObject.layer = layerNumber;
Example for your specific situation:
// Change Object1's GameObject to layer number 7
Object1.gameObject.layer = 7;
side note, Object1 could equally be typed as a GameObject and you should cache the transform component instead.*
For more information about cameras, please see the [documentation][2], specifically Depth Only.
If you wanted to draw a player's gun without letting it get clipped inside the environment, you would set one Camera at Depth 0 to draw the environment, and another Camera at Depth 1 to draw the weapon alone. The weapon Camera's Clear Flags should be set to to depth only. This will keep the graphical display of the environment on the screen, but discard all information about where each object exists in 3-D space. When the gun is drawn, the opaque parts will completely cover anything drawn, regardless of how close the gun is to the wall. [1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject-layer.html [2]: http://docs.unity3d.com/Documentation/Components/class-Camera.htmlDepth Only
It's important to note that physically the items still collide in the game world, but it isn't rendered as such. It is a workaround to handle the clipping issue. If you're worried about collision problems, the solution could be a lot tougher.
Good input @Wuzseen. Using collider.enabled / collider.isTrigger and raycasting or spherecasting on release to confirm space for positioning would be a quick solution.
Thank you, it worked. But now I have another problem. When I go to the wall it looks like it's not inside it. But when I let go the object, it stays inside.
That's part of the issue we were mentioning in the previous comments. I'd suggest you use raycasting to see where, from the player's point of view, to position a released object.
Your answer
Follow this Question
Related Questions
Cannot rotate an object around the center 5 Answers
Rotate an object back to its old rotation 1 Answer
Camera rotation around player while following. 6 Answers
Will this code move an object 2 Answers
Weapon Pickup and Drop 0 Answers