- Home /
Move object with collision
hello guys
I'am new @ unity so i need some help. I want to move a object from point a to point b but this with a other object. For example:
My object a hand picks up a box then walks (or floats) from that point lets call it point a to a other point lets call it b. Then drops it or whatever. What is the best way to do this i'm somewhat formiliar with javascript and c#.
Any suggestions of samples
thnx in advance
do you want to pick up the object with a button press or what are the circumstances. if so you could try doing and on button press and then parent the object you pick up to the hand it would then travel with the hand
Answer by Scribe · Jan 20, 2011 at 04:12 PM
var SpawnTo : Transform; //your hand
var Object1 : Transform; //what your picking up
var dist = 5; //distance at which you can pick things up
function Update () {
if(Input.GetKey(KeyCode.Q)){
if(Vector3.Distance(transform.position, Object1.position) < dist){
Object1.parent = SpawnTo;
Object1.transform.position = SpawnTo.transform.position;
Object1.transform.rotation = SpawnTo.transform.rotation;
}
}
}
i think this would work. basically it checks whether you pressed a button i chose 'Q' but you can choose whatever you want. it then looks at your distance from the object and whether you can "reach it" (in this case i chose a distance of 5)
if both of the cases are true the object is parented to the 'SpawnTo' which can be your hand and its position and rotation are also set to it.
(As Scribe recomended) Search the answers for Parenting.
Or take a look at FixedJoint.
thnx guys for the answers it really helpt gonna use the script from scribe thanks for that. Also gonna look into FixedJoint.
really appriciate it.
Hello Scribe i got a question when i use Q in the script i get a error: object reference not set to an instance of an object moveObject.Update() not formiliar with errors in unity yet, this is the error line if(Vector3.Distance(transform.position, Object.position) < dist) Any tips ?????
var SpawnTo : Transform; //your hand var Object1 : Transform; //what your picking up var dist = 5; //distance at which you can pick things up private var isHolding = false;
function Update () { if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.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
}
}
supped up my old script and this worked perfectly for me when my object that i want to move has a rigid bodythat usually has gravity
hope this helps
scribe
Hallo Scribe thnx for the solution your a life saver really appriciate your effort
you are welcome :) glad I could help if you feel your question has been answered please click the tick next to the accepted answer then people will know to look here if they have the same question as you (i also get reputation points :D)
Your answer
Follow this Question
Related Questions
changing pivot's placement 2 Answers
What am I doing wrong? 1 Answer
colliding with 2 objects at a time 1 Answer
Detect collision with specified object 1 Answer
Objects Touching? 1 Answer