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#: Convert unity javascript (unityscript) to C# 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 Convert unity javascript (unityscript) to C# - 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: