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 Bincredible · Apr 09, 2015 at 04:27 AM · c#javascripterrorcharacter controllerconvert

Trouble converting player controller from JS to C#

Hello, I am trying to convert a player controller script from Unity to C# since I'm making a transition to it. However, I've ran into a problem and I've used this to convert the script from JS to C#: http://www.m2h.nl/files/js_to_c.php When I've got the script back from the converter I tried my best to fix the errors that it gave me but I still have 6 of them. I will show you the original JS, the converted script (in C#), the converted script which I have edited to try to get rid of the errors, and my console showing all of the errors I have got. Any help to get rid of the errors is really appreciated.

Original JS:

var autoRotate : boolean = true; var maxRotationSpeed : float = 360; var hoirzontalAxis = "Horizontal"; var verticalAxis = "Vertical"; var jumpButton = "Jump"; var myCam : Camera; var on = true; var camFollow : Transform;

private var controller : CharacterController;

function Awake () { controller = GetComponent(CharacterController);

}

function Update () {

 var directionVector = new Vector3(Input.GetAxis(hoirzontalAxis), Input.GetAxis(verticalAxis), 0);

  
 if (directionVector != Vector3.zero) {
     // Get the length of the directon vector and then normalize it
     // Dividing by the length is cheaper than normalizing when we already have the length anyway
     var directionLength = directionVector.magnitude;
     directionVector = directionVector / directionLength;
      
     // Make sure the length is no bigger than 1
     directionLength = Mathf.Min(1, directionLength);
      
     // Make the input vector more sensitive towards the extremes and less sensitive in the middle
     // This makes it easier to control slow speeds when using analog sticks
     directionLength = directionLength * directionLength;
      
     // Multiply the normalized direction vector by the modified length
     directionVector = directionVector * directionLength;
 }
  
 // Rotate the input vector into camera space so up is camera's up and right is camera's right
 directionVector = myCam.transform.rotation * directionVector;
 

  
 // Rotate input vector to be perpendicular to character's up vector
 var camToCharacterSpace = Quaternion.FromToRotation(-myCam.transform.forward, transform.up);
 directionVector = (camToCharacterSpace * directionVector);
  
 // Apply the direction to the CharacterController

     controller.SimpleMove(directionVector * 10.0);
     
  
 // Set rotation to the move direction   
 if (autoRotate && directionVector.sqrMagnitude > 0.01) {
     var newForward : Vector3 = ConstantSlerp(
         transform.forward,
         directionVector,
         maxRotationSpeed * Time.deltaTime
     );
     newForward = ProjectOntoPlane(newForward, transform.up);
     transform.rotation = Quaternion.LookRotation(newForward, transform.up);
     
     
     
 }
  
 

}

function ProjectOntoPlane (v : Vector3, normal : Vector3) {

     return v - Vector3.Project(v, normal);
 

}

function ConstantSlerp (from : Vector3, to : Vector3, angle : float) {

 var value : float = Mathf.Min(1, angle / Vector3.Angle(from, to));
 return Vector3.Slerp(from, to, value);

}

Converted C#:

// Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden // Do test the code! You usually need to change a few small bits.

using UnityEngine; using System.Collections;

public class MYCLASSNAME : MonoBehaviour { bool autoRotate = true; float maxRotationSpeed = 360; FIXME_VAR_TYPE hoirzontalAxis= "Horizontal"; FIXME_VAR_TYPE verticalAxis= "Vertical"; FIXME_VAR_TYPE jumpButton= "Jump"; Camera myCam; FIXME_VAR_TYPE on= true; Transform camFollow;

private CharacterController controller;

void Awake (){ controller = GetComponent();

}

void Update (){

 FIXME_VAR_TYPE directionVector= new Vector3(Input.GetAxis(hoirzontalAxis), Input.GetAxis(verticalAxis), 0);

  
 if (directionVector != Vector3.zero) {
     // Get the length of the directon vector and then normalize it
     // Dividing by the length is cheaper than normalizing when we already have the length anyway
     FIXME_VAR_TYPE directionLength= directionVector.magnitude;
     directionVector = directionVector / directionLength;
      
     // Make sure the length is no bigger than 1
     directionLength = Mathf.Min(1, directionLength);
      
     // Make the input vector more sensitive towards the extremes and less sensitive in the middle
     // This makes it easier to control slow speeds when using analog sticks
     directionLength = directionLength * directionLength;
      
     // Multiply the normalized direction vector by the modified length
     directionVector = directionVector * directionLength;
 }
  
 // Rotate the input vector into camera space so up is camera's up and right is camera's right
 directionVector = myCam.transform.rotation * directionVector;
 

  
 // Rotate input vector to be perpendicular to character's up vector
 FIXME_VAR_TYPE camToCharacterSpace= Quaternion.FromToRotation(-myCam.transform.forward, transform.up);
 directionVector = (camToCharacterSpace * directionVector);
  
 // Apply the direction to the CharacterController

     controller.SimpleMove(directionVector * 10.0f);
     
  
 // Set rotation to the move direction   
 if (autoRotate && directionVector.sqrMagnitude > 0.01f) {
     Vector3 newForward = ConstantSlerp(
         transform.forward,
         directionVector,
         maxRotationSpeed * Time.deltaTime
     );
     newForward = ProjectOntoPlane(newForward, transform.up);
     transform.rotation = Quaternion.LookRotation(newForward, transform.up);
     
     
     
 }
  
 

}

void ProjectOntoPlane ( Vector3 v , Vector3 normal ){

     return v - Vector3.Project(v, normal);
 

}

void ConstantSlerp ( Vector3 from , Vector3 to , float angle ){

 float value = Mathf.Min(1, angle / Vector3.Angle(from, to));
 return Vector3.Slerp(from, to, value);

} }

My edited version of the converted script:

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public bool autoRotate = true;
 public float maxRotationSpeed = 360;
 public string hoirzontalAxis = "Horizontal";
 public string verticalAxis = "Vertical";
 public string jumpButton = "Jump";
 public Camera myCam;
 public bool on = true;
 public Transform camFollow;
 
 private CharacterController controller;
 
 void  Awake (){
     controller = GetComponent<CharacterController>();
 }
 
 void  Update (){
     
     Vector3 directionVector= new Vector3(Input.GetAxis(hoirzontalAxis), Input.GetAxis(verticalAxis), 0);
     
     
     if (directionVector != Vector3.zero) {
         // Get the length of the directon vector and then normalize it
         // Dividing by the length is cheaper than normalizing when we already have the length anyway
         var directionLength = directionVector.magnitude;
     
         directionVector = directionVector / directionLength;
         
         // Make sure the length is no bigger than 1
         directionLength = Mathf.Min(1, directionLength);
         
         // Make the input vector more sensitive towards the extremes and less sensitive in the middle
         // This makes it easier to control slow speeds when using analog sticks
         directionLength = directionLength * directionLength;
         
         // Multiply the normalized direction vector by the modified length
         directionVector = directionVector * directionLength;
     }
     
     // Rotate the input vector into camera space so up is camera's up and right is camera's right
     directionVector = myCam.transform.rotation * directionVector;
     
     
     
     // Rotate input vector to be perpendicular to character's up vector
     var camToCharacterSpace = Quaternion.FromToRotation(-myCam.transform.forward, transform.up);
     
     directionVector = (camToCharacterSpace * directionVector);
     
     // Apply the direction to the CharacterMotor
     controller.SimpleMove(directionVector * 0.4f);
     
     
     // Set rotation to the move direction   
     if (autoRotate && directionVector.sqrMagnitude > 0.01f) {
         Vector3 newForward = ConstantSlerp(
             transform.forward,
             directionVector,
             maxRotationSpeed * Time.deltaTime
             );
         newForward = ProjectOntoPlane(newForward, transform.up);
         transform.rotation = Quaternion.LookRotation(newForward, transform.up);
         
         
         
     }
     
     
 }
 
 void ProjectOntoPlane ( Vector3 v ,   Vector3 normal  ){
     
     return v - Vector3.Project(v, normal);
     
 }
 
 void ConstantSlerp ( Vector3 from ,   Vector3 to ,   float angle  ){
     
     float value = Mathf.Min(1, angle / Vector3.Angle(from, to));
     return Vector3.Slerp(from, to, value);
     
 }

}

The erros I get in the console, sorry if they are hard to see, you might need to zoom in: alt text

screen-shot-2015-04-09-at-181744.png (56.8 kB)
Comment
Add comment · Show 2
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 gjf · Apr 08, 2015 at 02:30 PM 0
Share

don't see any errors... without them it's going to be tough to help you.

avatar image Bincredible · Apr 09, 2015 at 05:14 PM 0
Share

How on earth did I forget to put the errors on. Sorry, I've put them on now.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by samtperrin · Apr 09, 2015 at 08:32 PM

Why not use the new rigidbody fps controller that comes in the sample assets for Unity 5? It is already in C#.

A lot of your errors are related to returning void instead of Vector3.

For example you have the following:

 void ProjectOntoPlane ( Vector3 v ,   Vector3 normal  )
 {
      
      return v - Vector3.Project(v, normal);
      
 }

It should be:

 Vector3 ProjectOntoPlane ( Vector3 v ,   Vector3 normal  ){
      
      return v - Vector3.Project(v, normal);
      
  }

This is because you return the type Vector3 and not void(nothing). That is just one example from your code. I will let you workout the other ones :)

Comment
Add comment · Show 2 · 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
avatar image Bincredible · Apr 10, 2015 at 12:10 AM 0
Share

Thanks for the help! :) I would never have worked that out.

avatar image samtperrin · Apr 10, 2015 at 05:44 AM 0
Share

When there is a 'return [value]' inside a function it must always return the type that [value] is.

Can you mark this as the correct answer please?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need help converting Javascript to C# 3 Answers

C# trigger problem 1 Answer

The name 'TrackManager' does not exist in the current context [Unity Vuforia Ground Plane Project] 0 Answers

TouchPad in C# 3 Answers

Converting a Javascript Drag and Drop script to C# 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