- Home /
Traslate C# to JS
Need traslate the example Locomotion System C# to Javascript. Help!
using UnityEngine; using System.Collections;
[RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(CapsuleCollider))] public class PhysicsCharacterMotor : CharacterMotor {
public GameObject character;
public float SpeedAnim;
public float maxRotationSpeed = 270;
public bool useCentricGravity = false;
public LayerMask groundLayers;
public Vector3 gravityCenter = Vector3.zero;
void Awake () {
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
character.animation["run"].speed = SpeedAnim;
character.animation["run"].layer = 1;
character.animation["fly"].layer = 1;
character.animation["idle"].layer = 1;
}
private void AdjustToGravity() {
int origLayer = gameObject.layer;
gameObject.layer = 2;
Vector3 currentUp = transform.up;
//Vector3 gravityUp = (transform.position-gravityCenter).normalized;
float damping = 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;
}
private void UpdateFacingDirection() {
// Calculate which way character should be facing
float facingWeight = desiredFacingDirection.magnitude;
Vector3 combinedFacingDirection = (transform.rotation * desiredMovementDirection * (1-facingWeight)+ desiredFacingDirection * facingWeight);
combinedFacingDirection = Util.ProjectOntoPlane(combinedFacingDirection, transform.up);
combinedFacingDirection = alignCorrection * combinedFacingDirection;
if (combinedFacingDirection.sqrMagnitude > 0.1f) {
Vector3 newForward = Util.ConstantSlerp(transform.forward,combinedFacingDirection,maxRotationSpeed*Time.deltaTime);newForward = Util.ProjectOntoPlane(newForward, transform.up);
//Debug.DrawLine(transform.position, transform.position+newForward, Color.yellow);
Quaternion q = new Quaternion();
q.SetLookRotation(newForward, transform.up);
transform.rotation = q;
}
}
private void UpdateVelocity() {
Vector3 velocity = rigidbody.velocity;
if (grounded) velocity = Util.ProjectOntoPlane(velocity, transform.up);
// Calculate how fast we should be moving
jumping = false;
if (grounded) {
// Apply a force that attempts to reach our target velocity
Vector3 velocityChange = (desiredVelocity - velocity);
if (velocityChange.magnitude > maxVelocityChange) {
velocityChange = velocityChange.normalized * maxVelocityChange;
}
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
character.animation.CrossFade("run", 0.05f);
// Jump
if (canJump && Input.GetButton("Jump")) {
rigidbody.velocity = velocity + transform.up * Mathf.Sqrt(2 * jumpHeight * gravity);
jumping = true;
character.animation.CrossFade("fly", 0.1f);
}
}
// Apply downwards gravity
rigidbody.AddForce(transform.up * -gravity * rigidbody.mass);
grounded = false;
}
void OnCollisionStay () {
grounded = true;
}
void FixedUpdate () {
if (useCentricGravity) AdjustToGravity();
UpdateFacingDirection();
UpdateVelocity();
}
}
Unity Answers is for specific questions. Try asking this in the Forums.
Answer by Lo0NuhtiK · Jan 19, 2012 at 06:23 PM
If you know a little UnityScript/JS then converting from c# to it isn't all-that-tricky if you can tell basically what you're looking at in the c# script. Play around with both of the languages sometimes, converting smaller scripts between the languages, and you'll pick up on the differences and start learning more about both of them.
@script RequireComponent(Rigidbody) //c#->[RequireComponent(typeof(Rigidbody))]
@script RequireComponent(CapsuleCollider)
/**
** difference between c# variable declaration and unity/js is simple.
** if you're a JS person, the c# variables seem a little backwards since
** the type is declared before the name. Then also, in the unity/js you
** don't have to specify if the variable is public because that's unity/js
** default setting for them. Also, of course, the "var" needs added to unity/js
**/
var character : GameObject ;
var SpeedAnim : float ;
var maxRotationSpeed : float = 270 ;
var useCentricGravity : boolean = false ; //other small differences like 'bool' for c# and 'boolean' for unity/js
var groundLayers : LayerMask ;
var gravityCenter : Vector3 = Vector3.zero ;
//Then with unity/js replace "void" with "function"
function Awake(){
rigidbody.freezeRotation = true ;
rigidbody.useGravity = false ;
character.animation["run"].speed = SpeedAnim ;
character.animation["run"].layer = 1 ;
character.animation["fly"].layer = 1 ;
character.animation["idle"].layer = 1 ;
}
private function AdjustToGravity(){
var origLayer : int = gameObject.layer ;
gameObject.layer = 2 ;
var currentUp : Vector3 = transform.up ;
//var gravityUp : Vector3 = (transform.position-gravityCenter).normalized;
var damping : float = Mathf.Clamp01(Time.deltaTime*5) ;
var hit : RaycastHit ;
//noting any of the differences yet? or are you just copy/pasting? lol
var desiredUp : Vector3 = Vector3.zero ;
for(var i : int = 0 ; i < 8 ; i++){
var rayStart : Vector3 = transform.position + transform.up + Quaternion.AngleAxis(360*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 : Vector3 = (currentUp+desiredUp*damping).normalized ;
var angle : float = Vector3.Angle(currentUp,newUp) ;
if(angle > 0.01){
var axis : Vector3 = Vector3.Cross(currentUp,newUp).normalized ;
var rot : Quaternion = Quaternion.AngleAxis(angle,axis) ;
transform.rotation = rot * transform.rotation ;
}
gameObject.layer = origLayer ;
}
/***********
** I'll let you give the rest a try. It looks like everything from here down
** is just changing the variables from something like
** public float this = 5.0f ;
** to
** var this : float = 5.0 ;
**
** and changing "void" to "function"
**
** that's pretty much all there was to this script other than the requirecomponent
** and the raycast's minor difference
***********/
if you still need help with the rest, then post back a comment after you've given it a shot or two and I or someone else will help. It will serve you well to try and pick up on how to translate between the two if you plan on using pre-made scripts but only one language throughout your project.
Your answer
Follow this Question
Related Questions
having prob while accessing java boolean from c# script 1 Answer
How to make a C# script to JavaScript 1 Answer
Is this C# correct in Unityscript? 1 Answer
What am I doing wrong 1 Answer