x


quit box collider?

Hi, i have a question. I have this script for take wooden boxes and move, but when they collide with another box or a wall, spin like crazy !! Is possible make the boxes transparent like Half Life, and quit the collider until press another time "q" ??

[sorry for my english, is very poor :-( ]

#pragma strict

private var pickObj: Transform = null;
private var hit: RaycastHit;
private var dist: float;
private var newPos: Vector3;

function Update(){
    if(Input.GetKeyDown("q") && pickObj!=null)
    {
       pickObj = null;
    }
    else if(Input.GetKeyDown("q") || pickObj!=null){ // if left button creates a ray from the mouse
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (!pickObj){ // if nothing picked yet...
            if (Physics.Raycast(ray, hit) && hit.transform.tag == "Pick"){
                // if it's a rigidbody, zero its physics velocity
                if (hit.rigidbody) hit.rigidbody.velocity = Vector3.zero;
                pickObj = hit.transform; // now there's an object picked
                // remember its distance from the camera
                dist = Vector3.Distance(pickObj.position, Camera.main.transform.position);
            }
        }
        else { // if object already picked...
            newPos = ray.GetPoint(dist); // transport the object
            pickObj.position = newPos;   // to the mouse position 
        }    
    }
}
more ▼

asked Jul 01 '12 at 04:55 PM

imrod gravatar image

imrod
0 1 2

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

2 answers: sort voted first

Yes I suggest you do this.

  • when picking up the object set the collider to disabled
  • possibly set the rigidbody to isKinematic
  • reset after
    function Update(){
    if(Input.GetKeyDown("q") && pickObj!=null)
    {
       pickObj.collider.enabled = true;
       pickObj = null;

    }
    else if(Input.GetKeyDown("q") || pickObj!=null){ // if left button creates a ray from the mouse
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (!pickObj){ // if nothing picked yet...
            if (Physics.Raycast(ray, hit) && hit.transform.tag == "Pick"){
                // if it's a rigidbody, zero its physics velocity
                if (hit.rigidbody) hit.rigidbody.velocity = Vector3.zero;
                pickObj = hit.transform; // now there's an object picked
                pickObj.collider.enabled = false;
                // remember its distance from the camera
                dist = Vector3.Distance(pickObj.position, Camera.main.transform.position);
            }
        }
        else { // if object already picked...
            newPos = ray.GetPoint(dist); // transport the object
            pickObj.position = newPos;   // to the mouse position 
        }    
    }
}
more ▼

answered Jul 01 '12 at 05:23 PM

whydoidoit gravatar image

whydoidoit
33k 11 23 100

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

thanks, but when drop the box, not have collider and falls towards the center of the earth :(

if i add "pickObj.collider.enabled = true;" at the end, not working, any suggestion?

(ah, the kinematic no, because the boxes are left floating in the air)

more ▼

answered Jul 01 '12 at 06:51 PM

imrod gravatar image

imrod
0 1 2

Please use the Add New Comment button on the right of the screen rather than adding an answer which is not a solution.

Jul 01 '12 at 06:55 PM whydoidoit

Yeah I got the ordering wrong, changing the answer

Jul 01 '12 at 06:57 PM whydoidoit
(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:

x1692
x161

asked: Jul 01 '12 at 04:55 PM

Seen: 326 times

Last Updated: Jul 01 '12 at 06:57 PM