More Trouble with magnet script.

Okay,

I have begun writing a new magnet script how ever I have ran into a few problems I can't seem to get around or find a solution to after a lot of rigorous searching....

  • Is there an input function that remains true if you press a key once? An example might be a light switch. You press "o" to turn it on and it remains on until you press "f" to turn it on. Is this possible how can I do it?
  • How might I parent the rigidbody named "Enemy" and seen as "var targetobject" in the script to the magnet?
  • Is this possible to do with a character controller as well?
  • Is there a way to have my magnet only affect objects on a specific layer?
  • Is there a way to move an object, rigidbody, or whatever to another layer, give it a new name, give it a new tag, or something through scripting while the game is running?
  • Could I use the LookAt function with the rigidbody so that it looks at the magnet and moves forward until it's position is very close to the magnet where the rigidbody will then snap(copy) the magnets position and be parented to the playerobject.

    I think that's about all the questions I have about this script. I am basically trying to make a magnet you can turn on or off (and it stays on or off unless the User intervenes). This magnet will attract the rigidbody and the rigidbody will become a child of the playerobject. The magnet will remain off while the rigidbody is a child. The magnet, by the way, is an empty so there are no collision problems :). Here's the code:


//This is a script to simulate magnetism. Activate with "a", Deactivate with "d".
// When the object being attracted comes close enough it will snap into position and...
// ...become a child of the magnetic object and the magnet will be deactivated.

function Update ()
{

    //Is the magnet on or off
    var On= false;

    //Here we get the position of the magnet and the object we want to attract
    var magnetposition= gameObject.transform.position;
    var targetobject= gameObject.Find("Enemy");
    var targetposition=targetobject.transform.position;

    //Here we get the how far apart the two objects are as a Vector3
    var distanceCoords=magnetposition-targetposition;

    //Convert Vector3 into integers by taking the absolute value of each axis (This way it won't be negative).
    var dx=Mathf.Abs(distanceCoords[0]);
    var dy=Mathf.Abs(distanceCoords[1]);
    var dz=Mathf.Abs(distanceCoords[2]);

    //This is the distance between the two objects represented by a single integer and the
    //minimum distance for the objects to "snap" together.

    var absoluteDistance=dx + dy + dz;
    var snapDistance= 1.4;

    //If the absolute distance is close enough to snap together we will parent the two objects
    if(absoluteDistance  snapDistance)
    {
        Debug.Log("Not Close Enough!");
    }
}

-Is there an input function that remains true if you press a key once? An example might be a light switch. You press "o" to turn it on and it remains on until you press "f" to turn it on. Is this possible how can I do it?

No - you'd set a bool to true on "o", and set it to false on "f". You then check for the bool in update to see whether you need to act on it

-How might I parent the rigidbody named "Enemy" and seen as "var targetobject" in the script to the magnet?

You'd move var targetobject : Transform; to the top of the script, and assign it in the editor. rip out the var targetobject= gameObject.Find("Enemy"); and you're sorted

-Is this possible to do with a character controller as well?

Yes

-Is there a way to have my magnet only affect objects on a specific layer?

Yes - check the layer is equal to the wanted layer when you're going through which objects to move

-Is there a way to move an object, rigidbody, or whatever to another layer, give it a new name, give it a new tag, or something through scripting while the game is running?

yourGameObject.name = "New Name";
yourGameObject.tag = "Predefined Tag";
yourGameObject.layer = 0;

-Could I use the LookAt function with the rigidbody so that it looks at the magnet and moves forward until it's position is very close to the magnet where the rigidbody will then snap(copy) the magnets position and be parented to the playerobject.

Yes - you'd just do theRigidbody.transform.LookAt(targetobject); when you've made targetobject a transform, and after moving the object, check if (distance < threshold) before setting the position to the target position

Edit to answer the questions in your answer (which really should just be edited into the question or added as a comment ;) )

-It is not possible to press "o" once and have the light remain on until I press "f" and vice versa?

That's exactly what using a bool would let you do, with pretty minimal code

-You said to assign the rigidbody via the Transform function, but if I were to have several unique "enemies" is there a way I could make this script affect any and all of them?

you could do something like this:

var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");

and then you'd loop through them (similar to the last script)

Thank you Mike,

This is exactly the type of response I was looking for, but just to clarify,

  • It is not possible to press "o" once and have the light remain on until I press "f" and vice versa?
  • You said to assign the rigidbody via the Transform function, but if I were to have several unique "enemies" is there a way I could make this script affect any and all of them?

again thank you so much this is perfect! Just wanna clarify the above subjects before I check the answered button for this thread.

Okay thank you I understand now

what would be an example of the bool and using an updater? or is there a resource you know of that could teach me more about them?

and for the second thing you clarified so I basicly make an array of enemies composed of guys with the tag "enemy"?

I'm so sorry if you feel like I'm twisting your arm for answers, but I really do appreciate it and it really is helpful. It's my hope that in time I can stop always asking questions and start answering them!