I’m using the DragRigidbody script. What I want to do is drag a cylinder to a certain spot between two tanks where an invisible game object is and have the cylinder snap into place like a pipe connecting the two tanks.
What happens is that the cylinder snaps into place and keeps spinning, I assume due to the DragRigidbody script still acting on it.
My code that is attached to the invisible game object (the transfom var is set in the IDE to be the cylinder):
var object : Transform;
var distanceLimit : float = 2.0;
function Update () {
if (Vector3.Distance(transform.position, object.position) < distanceLimit) {
object.DetachChildren();
object.Rotate(Vector3(0,0,270));
object.position = Vector3(-5.86,-4.76,-6.4);
}
}
This might be helpful. I have trouble figuring out rotations myself, so what I did was create an empty game object (called ego) and referenced to its rotation…
Just compare the coding(the drag rigidbody is unchanged, so just reimport this version)
Hopefully some better programmers than I can offer better advice, but if your needs are to get on with the job asap, this should cut it.
var WhereItBloodyShouldBe: Vector3;
var ego : Transform;
var object : Transform;
var distanceLimit : float = 2.0;
function Update () {
if (Vector3.Distance(transform.position, object.position) < distanceLimit)
BeerFunction();
}
function BeerFunction(){
object.DetachChildren();
//if your object has children...
object.transform.rotation=ego.transform.rotation;
object.transform.position = WhereItBloodyShouldBe;
rigidbody.freezeRotation = true;
}
I decided that the right thing to do when I get help is post the code that I ended up using myself.
I’m not using three objects as Targos example did. I just use the object that I want to “stick” in a certain location and position and the invisible GameObject that it “sticks to.”
Here is my final code (I kept the WhereItBloodyShouldBe var as it makes me smile):
#pragma strict
var WhereItBloodyShouldBe: Vector3;
var object : Transform; // must have a Transform AND a Rigidbody
var distanceLimit : float = 3.0;
function Update () {
if (Vector3.Distance(transform.position, object.position) < distanceLimit){
BeerFunction();
}
}
function BeerFunction(){
var objrb : Rigidbody = object.rigidbody;
object.DetachChildren();
object.transform.rotation=transform.rotation;
object.transform.position = WhereItBloodyShouldBe;
objrb.freezeRotation = true;
}