Adjust To Gravity

Would anyone help me translating this C# code to JS. i have tried but my skill’s in C# are not to brag about. heh

function AdjustToGravity() {
		var origLayer : int = gameObject.layer;
		gameObject.layer = 2;
 
		var currentUp : Vector3 = transform.up;
		//Vector3 gravityUp = (transform.position-gravityCenter).normalized;
 
		var damping : float = Mathf.Clamp01(Time.deltaTime*5);
 
		RaycastHit hit;
 
		Vector3 desiredUp = Vector3.zero;
		for (int i=0; i<8; i++) {
			Vector3 rayStart =
				transform.position
					+ transform.up
					+ Quaternion.AngleAxis(360*i/8.0f, transform.up)
						* (transform.right*0.5f)
					+ desiredVelocity*0.2f;
			if ( Physics.Raycast(rayStart, transform.up*-2, out hit, 3.0f, groundLayers.value) ) {
				desiredUp += hit.normal;
			}
		}
		desiredUp = (currentUp+desiredUp).normalized;
		Vector3 newUp = (currentUp+desiredUp*damping).normalized;
 
		float angle = Vector3.Angle(currentUp,newUp);
		if (angle>0.01) {
			Vector3 axis = Vector3.Cross(currentUp,newUp).normalized;
			Quaternion rot = Quaternion.AngleAxis(angle,axis);
			transform.rotation = rot * transform.rotation;
		}
 
		gameObject.layer = origLayer;
	}

had problem’s sticking the player to the ground and then i found this C# script in the locomotive demo, i think this is a good way to go… :slight_smile:

I changed it so it compiles; didn’t test it though. :wink:

function AdjustToGravity() { 
	var origLayer : int = gameObject.layer; 
	gameObject.layer = 2; 
	var currentUp = transform.up; 
	var damping : float = Mathf.Clamp01(Time.deltaTime*5); 
	var hit : RaycastHit; 	
	var desiredUp = Vector3.zero;

	for (i=0; i<8; i++) { 
		var rayStart = 
			transform.position 
			+ transform.up 
			+ Quaternion.AngleAxis(360.0*i/8.0, transform.up) 
			* (transform.right*0.5) 
			+ desiredVelocity*0.2; 
		if ( Physics.Raycast(rayStart, transform.up*-2, hit, 3.0, groundLayers.value) ) { 
			desiredUp += hit.normal; 
		} 
	} 
	desiredUp = (currentUp+desiredUp).normalized; 
	var newUp = (currentUp+desiredUp*damping).normalized; 
  
	var angle = Vector3.Angle(currentUp,newUp); 
	if (angle>0.01) { 
		var axis = Vector3.Cross(currentUp,newUp).normalized; 
		var rot = Quaternion.AngleAxis(angle,axis); 
		transform.rotation = rot * transform.rotation; 
	} 
  
	gameObject.layer = origLayer; 
}

–Eric