C# to JavaScript

I need some help converting some Scripts from C# to Javascript Im not sure if I’ll get any help on this one but I thought it would be worth a shot. Hopefully someone is really in a helping mood here is the first Script.

FauxGravityBody C#

using UnityEngine;

[RequireComponent (typeof(Rigidbody))]
public class FauxGravityBody : MonoBehaviour
{
	/*
	 * Class members
	 */
	public FauxGravityAttractor m_Attractor = null;
	private int m_Grounded = 0;
	
	
	/*
	 * Public grounded member accessor
	 */
	public int Grounded
	{
		get
		{
			// Return value of grounded member
			return this.m_Grounded;
		}
	}
	
	
	/*
	 * Private start class method
	 */
	private void Awake()
	{
		// Set needed rigid body values
		this.rigidbody.WakeUp();
		this.rigidbody.useGravity = false;
		this.rigidbody.freezeRotation = true;
	}
	
	
	/*
	 * Private on collision enter class method
	 */
	private void OnCollisionEnter(Collision c)
	{
		// Check the tag of collided object
		if(c.gameObject.tag == "WorldObject")
		{
			// Increase grounded member
			this.m_Grounded++;
		}
	}
	
	
	/*
	 * Private on collision exit class method
	 */
	private void OnCollisionExit(Collision c)
	{
		// Check the tag of collided object and grounded member value
		if(c.gameObject.tag == "WorldObject"  this.m_Grounded > 0)
		{
			// Decrease grounded member
			this.m_Grounded--;
		}
	}
	
	
	/*
	 * Private fixed update class method
	 */
	private void FixedUpdate()
	{
		// Check if attractor member is set
		if(this.m_Attractor != null)
		{
			// Apply attract to object
			this.m_Attractor.Attract(this);
		}
	}
}

And at last my efforts which is a struggle but I think Im getting closer…but still have a long way to go…(sigh…)

@script RequireComponent(Rigidbody)

private  var m_Grounded : int = 0;

function Awake () 
{
	// Set needed rigid body values
	this.rigidbody.WakeUp();
	this.rigidbody.useGravity = false;
	this.rigidbody.freezeRotation = true;
}
[COLOR="red"]function FauxGravityBody ()
{
	var m_Attractor : FauxGravityAttractor= null;
}

function Grounded(int)
{
	// Return value of grounded member
	return this.m_Grounded;
	
}[/COLOR]
function OnCollisionEnter ( c : Collision  )
{
	// Check the tag of collided object
	if(c.gameObject.tag == "WorldObject")
	{
		// Increase grounded member
		this.m_Grounded++;
	}
}
function OnCollisionExit ( c : Collision  ) 
{
	// Check the tag of collided object and grounded member value
	if(c.gameObject.tag == "WorldObject"  this.m_Grounded > 0)
	{
		// Decrease grounded member
		this.m_Grounded--;
	}
}
function FixedUpdate () 
{
	// Check if attractor member is set
	if(this.m_Attractor != null)
	{
		// Apply attract to object
		this.m_Attractor.Attract(this);
	}
}

The ones in Red Im pretty sure are wrong but honestly I dont know what to do with them.
There are also a few parts of the script Im unsure of like the “public int Grounded” ?
I can also attach a project if it would help?

what is the debugger saying? what error?

Here are the errors:

The last error is from another script I tried to convert from C# to Javascript

FauxGravityAttractor.js

var m_UseLocalVector : boolean= true;
var m_Gravity : float = -10.0;
	
	
function Attract ( body : FauxGravityBody  ) 
{
	// Create local variables
	 var gravityUp : Vector3;
	 var trans : Transform= body.transform;
	 var rigid : Rigidbody= body.rigidbody;

	// Set gravity local variable
	gravityUp = trans.position - this.transform.position;
	gravityUp.Normalize();
		
	// Apply forces to body
	rigid.AddForce(gravityUp * this.m_Gravity * rigid.mass);
		
	// Check value of bodys grounded value
	if(body.Grounded >= 1)
	{
		// Set rigid body drag
		rigid.drag = 0.1;
	}
	else
	{
		// Set rigid body drag
		rigid.drag = 1.0;
	}
		
	// Check value of rotation freeze
	if(rigid.freezeRotation == true)
	{
		// Calculate quaternion
		 var quatern : Quaternion= Quaternion.FromToRotation(trans.up, gravityUp);
		quatern = quatern * trans.rotation;
			
		// Calculate rotation of body
		trans.rotation = Quaternion.Slerp(trans.rotation, quatern, 0.1);
		
	}
}
//FauxGravityBody.js

var m_Attractor : FauxGravityAttractor = null;
private var m_Grounded : int= 0;
function get Grounded() : int { return this.m_Grounded; }

function OnCollisionEnter ( c : Collision  ){
	// Check the tag of collided object
	if(c.gameObject.tag == "WorldObject")
	{
		// Increase grounded member
		this.m_Grounded++;
	}
}
function OnCollisionExit ( c : Collision  ) {
	// Check the tag of collided object and grounded member value
	if(c.gameObject.tag == "WorldObject"  this.m_Grounded > 0)
	{
		// Decrease grounded member
		this.m_Grounded--;
	}
}
function FixedUpdate () {
	// Check if attractor member is set
	if(this.m_Attractor != null)
	{
		// Apply attract to object
		this.m_Attractor.Attract(this);
	}
}
//FauxGravityAttractor.js
var m_UseLocalVector : boolean= true;
var m_Gravity : float = -10.0;
	
	
function Attract ( body : FauxGravityBody  ) 
{
	// Create local variables
	 var gravityUp : Vector3;
	 var trans : Transform= body.transform;
	 var rigid : Rigidbody= body.rigidbody;

	// Set gravity local variable
	gravityUp = trans.position - this.transform.position;
	gravityUp.Normalize();
		
	// Apply forces to body
	rigid.AddForce(gravityUp * this.m_Gravity * rigid.mass);
		
	// Check value of bodys grounded value
	if(body.Grounded >= 1)
	{
		// Set rigid body drag
		rigid.drag = 0.1;
	}
	else
	{
		// Set rigid body drag
		rigid.drag = 1.0;
	}
		
	// Check value of rotation freeze
	if(rigid.freezeRotation == true)
	{
		// Calculate quaternion
		 var quatern : Quaternion= Quaternion.FromToRotation(trans.up, gravityUp);
		quatern = quatern * trans.rotation;
			
		// Calculate rotation of body
		trans.rotation = Quaternion.Slerp(trans.rotation, quatern, 0.1);
		
	}
}

Sometimes the conversion is tricky. This is one of the reasons I would suggest going ahead and learning C# because it is well more documented.

This is from my little keeper file:

    private var moo : int = 1;
    function get Moo() : int { return moo; }
    function set Moo(value : int) { moo = value; }

BigMisterB You Are AWESOME!!!

I get these 2 errors?

I tried converting this last script

using UnityEngine;

public class PlayerGravityBody : FauxGravityBody
{
	/*
	 * Class members
	 */
	public float m_MaxSpeed = 4.5f;
	public float m_Force = 8.0f;
	public float m_JumpSpeed = 5.0f;
	private bool m_Jumping = false;
	
	/*
	 *  Public horizontal force accessor
	 */
	public float HorizontalForce
	{
		get
		{
			// Return the calculated horizontal force
			return Input.GetAxis("Horizontal") * this.m_Force;
		}
	}
	
	
	/*
	 * Public vertical force accessor
	 */
	public float VerticalForce
	{
		get
		{
			// Return the caluclated vertical force
			return Input.GetAxis("Vertical") * this.m_Force;
		}
	}
	
	
	/*
	 * Private update class method
	 */
	private void Update()
	{
		// Check if jump button was pressed and ridig body is grounded
		if(Input.GetButtonDown("Jump") == true  this.Grounded >= 1)
		{
			// Set jumping member value;
			this.m_Jumping = true;
		}
	}
	
	
	/*
	 * Private fixed update class method
	 */
	private void FixedUpdate()
	{
		// Check if attractor member is set
		if(this.m_Attractor != null)
		{
			// Apply attract to object
			this.m_Attractor.Attract(this);
		}
		
		// Check if rigid body velocity is within limits and grounded
		if(this.rigidbody.velocity.magnitude < this.m_MaxSpeed  this.Grounded >= 1)
		{
			// Apply forces to rigid body
			this.rigidbody.AddForce(this.transform.rotation * Vector3.forward * this.VerticalForce);
			this.rigidbody.AddForce(this.transform.rotation * Vector3.right * this.HorizontalForce);
		}
		
		// Check the state of jumping member
		if(this.m_Jumping == true)
		{
			// Apply jump forces
			this.rigidbody.velocity = this.rigidbody.velocity + (this.rigidbody.transform.up * this.m_JumpSpeed);
			
			// Set jumping member value
			this.m_Jumping = false;
		}
	}
}

and following your advice I came up with this

var m_MaxSpeed : float= 4.5;
var m_Force : float= 8.0;
var m_JumpSpeed : float= 5.0;
private  var m_Jumping : boolean= false;

function get HorizontalForce() { return Input.GetAxis("Horizontal") * this.m_Force; }
function get VerticalForce() { return Input.GetAxis("Vertical") * this.m_Force;	}

function Update () 
{
	// Check if jump button was pressed and ridig body is grounded
	if(Input.GetButtonDown("Jump") == true  this.Grounded >= 1)
	{
		// Set jumping member value;
		this.m_Jumping = true;
	}
}
	
function FixedUpdate () 
{
	// Check if attractor member is set
	if(this.m_Attractor != null)
	{
		// Apply attract to object
		this.m_Attractor.Attract(this);
	}
		
	// Check if rigid body velocity is within limits and grounded
	if(this.rigidbody.velocity.magnitude < this.m_MaxSpeed  this.Grounded >= 1)
	{
		// Apply forces to rigid body
		this.rigidbody.AddForce(this.transform.rotation * Vector3.forward * this.VerticalForce);
		this.rigidbody.AddForce(this.transform.rotation * Vector3.right * this.HorizontalForce);
	}
		
	// Check the state of jumping member
	if(this.m_Jumping == true)
	{
		// Apply jump forces
		this.rigidbody.velocity = this.rigidbody.velocity + (this.rigidbody.transform.up * this.m_JumpSpeed);
			
		// Set jumping member value
		this.m_Jumping = false;
	}
}

Im also attaching the project with one Scene in C# and the other Scene modified in Javascript

I do plan on learning C# because Im sure it would greatly improve my knowledge of coding but at the moment javascript is a handful (maybe I can dedicate my next game to C# only) just to learn both :slight_smile:

699479–25196–$CSharpToJavaScript.zip (144 KB)