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!");
}
}