x


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

more ▼

asked Jun 20 '12 at 03:39 AM

DarKKendO gravatar image

DarKKendO
48 2 5 5

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.

Jun 20 '12 at 03:57 AM Ampler Games

Tried that and thought that solved the problem. Then the walls started floating away!! haha. Might have to play around with it a bit today. Might mess around with some more physics ray casting and see what happens also

Jun 20 '12 at 09:36 AM DarKKendO

Hey. I'm having the exact same problem. Have you found a solution yet?

Aug 18 '12 at 04:03 PM Unnamed
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Aug 23 '12 at 09:07 PM

DarKKendO gravatar image

DarKKendO
48 2 5 5

(comments are locked)
10|3000 characters needed characters left

Does the cube have "Is Trigger" enabled?

more ▼

answered Jun 20 '12 at 04:36 AM

djm gravatar image

djm
175 3

Theres no triggers attached to the cube. Its just the standard one with a "pickable" tag added

Jun 20 '12 at 09:35 AM DarKKendO
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1947
x1578
x298
x28

asked: Jun 20 '12 at 03:39 AM

Seen: 476 times

Last Updated: Aug 23 '12 at 09:07 PM