Magnet Code from C# to JavaScript? Can I make it affect character controllers too?

Hello, After many frustrating hours trying to convert this code:

using UnityEngine;
using System.Collections;

public class Magnetic : MonoBehaviour
{
    public LayerMask m_MagneticLayers;
    public Vector3 m_Position;
    public float m_Radius;
    public float m_Force;

    void FixedUpdate ()
    {
        Collider[] colliders;
        Rigidbody rigidbody;

        colliders = Physics.OverlapSphere (transform.position + m_Position, m_Radius, m_MagneticLayers);
        foreach (Collider collider in colliders)
        {
            rigidbody = (Rigidbody) collider.gameObject.GetComponent (typeof (Rigidbody));
            if (rigidbody == null)
            {
                continue;
            }
            rigidbody.AddExplosionForce (m_Force * -1, transform.position + m_Position, m_Radius);
        }
    }

    void OnDrawGizmosSelected ()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere (transform.position + m_Position, m_Radius);
    }
}

to a Javascript, this is what I came up with:

var MagnetLayer : LayerMask;

var FieldPosition : Vector3;

var FieldRadius : int;

var FieldForce : int;


function FixedUpdate () {
    var colliders : Collider;
    var rigidbody : Rigidbody;

    colliders=Physics.OverlapSphere (transform.position + FieldPosition, FieldRadius, MagnetLayer);
    for(colliders in Collider){

        rigidbody(collider.gameObject.GetComponent(Rigidbody));

        if(Rigidbody== null )
        {
            continue;
        }
        rigidbody.AddExplosionForce (FieldForce * -1, transform.position + FieldPosition, FieldRadius);
    }
}

function OnDrawGizmosSelected()
{
    Gizmos.color= Color.red;
    Gizmos.DrawWireSphere (transform.position, FieldRadius);
}

Why is this still showing me error messages? I am no expert on coding, but I am attempting to get better with these little projects and so some help would be greatly appreciated. The original script was written by AngryAnt and it makes a magnetic field around your player that can attract rigidbodies to you. I want to adapt it to java so I can better understand it and modify it. Ideally I would like to attract not just rigidbodies but charactercontrollers or certain objects with a specific tag or any object within the magnetic layer or something. Furthermore in my java script I can't see the gizmo being drawn like I can in the c# script. Thanks a lot to everyone who replies, unity can be very challenging and I am just starting out! I hope this code will help others as well.

Try this:

var MagnetLayer : LayerMask;
var FieldPosition : Vector3;
var FieldRadius : float;
var FieldForce : float;

function FixedUpdate () {

    var colliders : Collider[];
    var rigidbody : Rigidbody;

    colliders=Physics.OverlapSphere (transform.position + FieldPosition, FieldRadius, MagnetLayer);

    for(var collider in colliders){

        rigidbody = collider.rigidbody;

        if(rigidbody== null )
        {
            continue;
        }
        rigidbody.AddExplosionForce (FieldForce * -1, transform.position + FieldPosition, FieldRadius);
    }
}

function OnDrawGizmosSelected()
{
    Gizmos.color= Color.red;
    Gizmos.DrawWireSphere (transform.position, FieldRadius);
}

I changed the radius and force values back to floats, ints would be a little strange there. On top of that, fixed the for loop, and the rigidbody assign, so those are the main areas to look at

To get it working with charactercontrollers, you would have to do a bit more work - there isn't an easy way to add an explosive force to one (that i know of)

You could however move by a certain amount each frame, e.g.:

for(var collider in colliders){

    var charCollider : CharacterController = collider.GetComponent(CharacterController);
    if (charCollider != null)
    {
        charCollider.Move( (transform.position - collider.transform.position) * Time.deltaTime * FieldPull);
    }
    rigidbody = collider.rigidbody;

    if(rigidbody== null )
    {
        continue;
    }
    rigidbody.AddExplosionForce (FieldForce * -1, transform.position + FieldPosition, FieldRadius);
}

CharacterControllers are specialized classes that don't obey the normal laws of physics systems.

You can, however, use rigidbodies and some scripting to accomplish almost the same thing, and also give yourself the ability to do things like add forces to them.

See this script as an example: http://www.unifycommunity.com/wiki/index.php?title=RigidbodyFPSWalker

I tried out Mike's code and I still get an error message of: Assets/Magnetism.js(11,37): BCE0022: Cannot convert '(UnityEngine.Collider)' to 'UnityEngine.Collider'.

I'm not sure why though...any thoughts?

That did it Mike thank you very much! What exactly do the [] brackets do?

sorry, whats the funcion of the MagnetLayer ?