Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kikuu · Jan 19, 2012 at 05:30 PM ·

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();
 }
 

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karl_ · Jan 19, 2012 at 06:15 PM 0
Share

Unity Answers is for specific questions. Try asking this in the Forums.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Moving from JS to C#, what should I know? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges