x


Picking up Objects

Hey,

Tried looking around but could not find anything that completely helped me out. I am trying to add a simple function that allows the player to pick up move and drop objects. I have a rigid body object in the scene and the first person controller colideing with it, but I am quite new to code and can't get very far with that code of things.

What I have so far, if its right at all.

function OnMouseDown() {

   if(distance <= 4.0) {


   }
}

Any help on this would be great.

Thanks

more ▼

asked Jul 27 '11 at 12:02 PM

matseffect gravatar image

matseffect
1 1 1 1

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

3 answers: sort voted first
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 = 5; //distance at which you can pick things up
private var isHolding = false;


function Update () {
    if(Input.GetKeyDown(KeyCode.Q)){ //if you press 'q'
        if(Vector3.Distance(transform.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
        }
        else{ //if isHolding isn't true
            SpawnTo.transform.DetachChildren(); //detach child (object) from hand
            Object1.rigidbody.useGravity = true; //add the gravity back on
        }
}

hope that works, I made this script for a previous question, just set the variable 'dist' to 4 like in your script

scribe

more ▼

answered Jul 27 '11 at 01:25 PM

Scribe gravatar image

Scribe
1.8k 12 16 34

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

Looks like your doing the right thing so far. But instead use Vector3.Distance(transform.position, theOtherObject.transform.position) instead of just distance. Because that will not do anything, you never assigned it as a variable. I will make a script for you sometime later in the day.

more ▼

answered Jul 27 '11 at 01:26 PM

ConfinedDarkness gravatar image

ConfinedDarkness
81 3 3 4

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

Thanks guys,

I will give that a go and see how I get on.

more ▼

answered Jul 27 '11 at 03:59 PM

matseffect gravatar image

matseffect
1 1 1 1

(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:

x5

asked: Jul 27 '11 at 12:02 PM

Seen: 1446 times

Last Updated: Jul 27 '11 at 03:59 PM