Search Unity

can disable script with script?

Discussion in 'Scripting' started by peptoabysmal, Nov 15, 2007.

  1. peptoabysmal

    peptoabysmal

    Joined:
    Oct 4, 2007
    Posts:
    33
    Hi,

    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):
    Code (csharp):
    1. var object : Transform;
    2. var distanceLimit : float = 2.0;
    3. function Update () {
    4.     if (Vector3.Distance(transform.position, object.position) < distanceLimit) {
    5.         object.DetachChildren();
    6.         object.Rotate(Vector3(0,0,270));
    7.         object.position = Vector3(-5.86,-4.76,-6.4);       
    8.     }
    9. }
    10.  
    The URL to see my experiment is:
    [url http://pontia.ucdavis.edu/simtest/brewsim3.html]
    http://pontia.ucdavis.edu/simtest/brewsim3.html
    [/url]

    Any help greatly appreciated :)

    eta: I stink at using these forum tools

    Also: It is dopey that I'm calling DetachChildren() because the script is not a child d'oh.

    I can't figure out how to access the script and turn it off basically - or maybe I'm totally going about this the wrong way.
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Theres a few issues with your script.

    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.
    Code (csharp):
    1. var WhereItBloodyShouldBe: Vector3;
    2. var ego : Transform;
    3. var object : Transform;
    4. var distanceLimit : float = 2.0;
    5. function Update () {
    6.    if (Vector3.Distance(transform.position, object.position) < distanceLimit)
    7.  
    8. BeerFunction();
    9. }
    10. function BeerFunction(){
    11.       object.DetachChildren();
    12. //if your object has children...
    13.      object.transform.rotation=ego.transform.rotation;
    14.       object.transform.position = WhereItBloodyShouldBe;      
    15.  
    16. rigidbody.freezeRotation = true;
    17. }
    Keep having fun!
    AC
     

    Attached Files:

  3. peptoabysmal

    peptoabysmal

    Joined:
    Oct 4, 2007
    Posts:
    33
    Targos, you da man!

    Thank you for the delicious pikelet recipe! :wink:

    I'm not from NZ, note my new avatar in honor of NZ, though.

    If you're in Calif. look me up, I owe ya!

    p.s. I'll worry about a better script after the wizard gives me a brain. This will do nicely.
     
  4. peptoabysmal

    peptoabysmal

    Joined:
    Oct 4, 2007
    Posts:
    33
  5. peptoabysmal

    peptoabysmal

    Joined:
    Oct 4, 2007
    Posts:
    33
    My apologies for bumping this dead thread.

    But...

    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):
    Code (csharp):
    1. #pragma strict
    2. var WhereItBloodyShouldBe: Vector3;
    3. var object : Transform; // must have a Transform AND a Rigidbody
    4. var distanceLimit : float = 3.0;
    5. function Update () {
    6.    if (Vector3.Distance(transform.position, object.position) < distanceLimit){
    7.         BeerFunction();
    8.    }
    9. }
    10. function BeerFunction(){
    11.         var objrb : Rigidbody = object.rigidbody;
    12.       object.DetachChildren();
    13.       object.transform.rotation=transform.rotation;
    14.       object.transform.position = WhereItBloodyShouldBe;      
    15.         objrb.freezeRotation = true;
    16. }