- Home /
Picked up cube passes through walls
Hey guys, Been searching the internet for hours and I can't figure out why this is happening. I have a scene with a first person character controller and a cube. The floor and 4 walls are all just stretched cubes. I have this script attached to the camera within the first person controller
using UnityEngine;
using System.Collections;
public class PickUpObjectTest : MonoBehaviour {
private Rigidbody rigid; //For storing the rigidbody if successfully hit
private int holdDistance; //The distance that the object will be held away from the player
private enum holdState {Free, Held}; //States in which the player can be regarding external objects
private holdState state; //State variable
private float increment; //The speed at which the object moves towards the camera
private Quaternion rotation; //The desired rotation of the object when picked up
// Use this for initialization
void Start () {
holdDistance = 3;
state = holdState.Free;
increment = 1.0f;
}
// Update is called once per frame
void FixedUpdate () {
switch (state){
case holdState.Free:
if (Input.GetKey ("e")){
Ray ray = camera.ScreenPointToRay(new Vector3(camera.pixelWidth/2, camera.pixelHeight/2, 0));
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 10)){
if (hit.transform.tag == "Pickable"){
rigid = hit.rigidbody;
state = holdState.Held;
}
}
}
break;
case holdState.Held:
Vector3 nextPos;
if (increment < 100.0f)
increment++;
rigid.transform.parent = transform;
nextPos = Vector3.Lerp(rigid.position, transform.position + transform.forward * holdDistance, increment);
if (!Physics.Raycast(rigid.position, nextPos, 2)){
rigid.position = nextPos;
}
//Rotation of the object
Vector3 direction = transform.position - rigid.transform.position;
rotation = Quaternion.LookRotation(direction);
rigid.transform.rotation = Quaternion.Slerp(rigid.transform.rotation, rotation, increment);
rigid.transform.rotation = Quaternion.Euler(rigid.transform.rotation.x,
rigid.transform.rotation.eulerAngles.y, rigid.transform.rotation.eulerAngles.z);
rigid.useGravity = false;
if (Input.GetKey ("e")){
increment = 1.0f;
rigid.useGravity = true;
rigid.transform.parent = null;
state = holdState.Free;
}
break;
}
}
}
I can pick up the cube and move around the scene with it however when I look down at the ground or walk near a wall the cube passes right through it. I have tried walking very slowly towards the wall and the cube doesn't immediately pass through, rather it pops through when I get close enough. Any help would be IMMENSELY appreciated as I am trying to hold on to the few remaining strands of hair I have left :(
Thanks Guys
I had a very similar problem with the drag ridgidbody script, I don't know why this happens but I found that if I made the wall have a ridgedbody but without gravity then change the wieght of the walls so they are really heavy, this fixed by problem.
Tried that and thought that solved the problem. Then the walls started floating away!! haha. $$anonymous$$ight have to play around with it a bit today. $$anonymous$$ight mess around with some more physics ray casting and see what happens also
Hey. I'm having the exact same problem. Have you found a solution yet?
Answer by DarKKendO · Aug 23, 2012 at 09:07 PM
Solved this issue. I found the answer by modifying the script found in this thread:
http://forum.unity3d.com/threads/79352-Half-Life-2-Object-Grabber
Answer by djm · Jun 20, 2012 at 04:36 AM
Does the cube have "Is Trigger" enabled?
Theres no triggers attached to the cube. Its just the standard one with a "pickable" tag added
Your answer
Follow this Question
Related Questions
Make a rolling ball always on ground without falling when reaching the edges of the map 1 Answer
Physics.SphereCastAll with triggers 1 Answer
Raycasting to the bottom side of a plane? 2 Answers
Physics.CapsuleCast Help 1 Answer
Raycast stops working after ~40 seconds when target has Rigidbody 2 Answers