- Home /
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:
don't see any errors... without them it's going to be tough to help you.
How on earth did I forget to put the errors on. Sorry, I've put them on now.
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 :)
Thanks for the help! :) I would never have worked that out.
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?