Need help converting Js to C#

Hi I need help converting Js to C# . I have tried but I failed.

Js :

//This script controls how the soldier will move.
@script RequireComponent(CharacterController);

var forwardSpeedMultiplier : float = 3.0;
var strafeSpeedMultiplier : float = 2.0;
var turnSpeedMultiplier : float = 6.0;
var gravity : float = 9.8;
//var soldierLocation : String = "smoothWorldPosition/soldierSkeleton";
var soldier : Transform;
var turnSpeed : float = 0.0;
var forwardSpeed : float = 0.0; //Speed the character will moved at.
var strafeSpeed : float = 0.0;
var isGrounded : boolean;

private var stopAfterLanding : float = 0.0; //How much time in seconds will the character stop after landing.
private var fallSpeed : float = 0.0;
private var lastGroundedTime : float; //Last time since soldier was touching the ground.
private var lastLandingTime : float; //Last time since the soldier landed after a fall.
private	var targetForwardSpeed : float;
private	var targetStrafeSpeed : float;
private var crouchControllerScript : crouchController;
private var isFalling : boolean;
private var healthScript : health;
private var recoilAmount : float;
private var recoilAmountTarget : float;

function Start(){
	crouchControllerScript = GetComponent("crouchController");
	healthScript = GetComponent("health");
	isFalling = false;
	//soldier = transform.Find(soldierLocation);
}

function Update () {
	//Stick to platforms.
	var rayHeight : float = 0.5;
	var rayOrigin : Vector3 = transform.position + Vector3.up * rayHeight;
	var platformRay : Ray = new Ray(rayOrigin, Vector3.down);
	var rayDistance : float = rayHeight * 2.0;
	var platformHit : RaycastHit;
	if (Physics.Raycast(platformRay, platformHit, rayDistance)){
		if(platformHit.transform.root != transform){
			isGrounded = true;
			transform.position.y = platformHit.point.y;
		}
	}
	else{
		isGrounded = false;
	}
	
	var controller : CharacterController = GetComponent(CharacterController);
	//Hit recoil.
	var recoilVector : Vector3 = Vector3.zero;
	var recoilInhibit : float = 1.0;
	var deathInhibit : float = 1.0;
	if (healthScript != null){
		var lastHitTime : float = healthScript.GetLastHitTime();
		var recoilDirecion : Vector3 = healthScript.GetrecoilDirecion();
		var maxRecoil : float = 0.6;
		var timeAfterHit : float = Time.time - lastHitTime;
		var recoilAmountTarget : float = 0.0;
		if(timeAfterHit < maxRecoil && lastHitTime != 0){
			recoilAmountTarget = maxRecoil-timeAfterHit;
			recoilAmountTarget *= (1-Mathf.Abs(Input.GetAxis("Vertical")));
			recoilAmountTarget *= (1-Mathf.Abs(Input.GetAxis("Horizontal")));
		}
		if(recoilAmount < recoilAmountTarget){
			recoilAmount = Mathf.Lerp(recoilAmount, recoilAmountTarget, Time.deltaTime);
		}
		else{
			recoilAmount = Mathf.Lerp(recoilAmount, recoilAmountTarget, Time.deltaTime*20);
		}
		var biasRecoilAmount : float = recoilAmount; //Bias to 0.
		biasRecoilAmount /= maxRecoil;
		biasRecoilAmount = Mathf.Pow(biasRecoilAmount,2.0);
		biasRecoilAmount *= maxRecoil;
		recoilVector = recoilDirecion * biasRecoilAmount;
		recoilInhibit = 1 -(recoilAmount*0.65/maxRecoil);
		var health : float = healthScript.health;
		if(health <= 0){
			deathInhibit = 0;
		}
		else{
			deathInhibit = 1.0;
		}
	}
	//Position.
	if (isGrounded){
		if (isFalling){
			isFalling = false;
			lastLandingTime = Time.time;
			stopAfterLanding = (lastLandingTime - lastGroundedTime) * 2.0;
		}
		fallSpeed = 0.0;
	}
	else{
		if (!isFalling){
			isFalling = true;
			lastGroundedTime = Time.time;
		}
		fallSpeed += gravity * Time.deltaTime;
	}
	var moveDirection : Vector3;
	moveDirection.y -= fallSpeed;
	if (isGrounded){
		targetForwardSpeed = Input.GetAxis("Vertical");
		targetStrafeSpeed = Input.GetAxis("Horizontal");
		targetForwardSpeed *=  forwardSpeedMultiplier;
		targetStrafeSpeed *= strafeSpeedMultiplier;
		if(Input.GetAxis("Vertical") < 0){//Slow down going backwards;
			targetForwardSpeed *= 0.5;
		}
		if(Input.GetKey(KeyCode.LeftShift)){//Sprint with left shift;
			targetForwardSpeed *= 1.5;
			targetStrafeSpeed *= 1.5;
		}
	}
	if(crouchControllerScript != null){ //Crouch speed multiplier.
		targetForwardSpeed = Mathf.Lerp(targetForwardSpeed, targetForwardSpeed * crouchControllerScript.crouchSpeedMultiplier, crouchControllerScript.globalCrouchBlend);
	}
	if(Time.time <= lastLandingTime + stopAfterLanding && stopAfterLanding > 0.5){
		var timeSinceLanding : float = Time.time - lastLandingTime;
		var landingSpeedInhibit : float = Mathf.Pow(timeSinceLanding / stopAfterLanding, 1.5);
		targetForwardSpeed *= landingSpeedInhibit;
		targetStrafeSpeed *= landingSpeedInhibit;
	}
	forwardSpeed = Mathf.Lerp(forwardSpeed, targetForwardSpeed, Time.deltaTime * 15.0);
	strafeSpeed = Mathf.Lerp(strafeSpeed, targetStrafeSpeed, Time.deltaTime * 15.0);
	moveDirection += soldier.forward * forwardSpeed * recoilInhibit * deathInhibit;
	moveDirection += soldier.right * strafeSpeed * recoilInhibit * deathInhibit;
	moveDirection += recoilVector;
	controller.Move(moveDirection * Time.deltaTime);//Move the controller.
	//Rotation.
	var targetTurnSpeed : float = 0.0;
	targetTurnSpeed= Input.GetAxis("Mouse X");
	targetTurnSpeed *= Mathf.Pow(turnSpeedMultiplier,3);
	targetTurnSpeed *= deathInhibit;
	turnSpeed = Mathf.Lerp(turnSpeed, targetTurnSpeed, Time.deltaTime * 25.0);
	transform.rotation.eulerAngles.y += turnSpeed * Time.deltaTime;
}

here is the FAIL C# that I have converted .

using UnityEngine;
using System.Collections;

public class Char_Movement : MonoBehaviour {
//This script controls how the soldier will move.

public float forwardSpeedMultiplier = 3.0f;
public float strafeSpeedMultiplier = 2.0f;
public float turnSpeedMultiplier = 6.0f;
public float gravity = 9.8f;
//string soldierLocation = "smoothWorldPosition/soldierSkeleton";
Transform soldier;
public float turnSpeed = 0.0f;
public float forwardSpeed = 0.0f; //Speed the character will moved at.
public float strafeSpeed = 0.0f;
public bool isGrounded;

private float stopAfterLanding = 0.0f; //How much time in seconds will the character stop after landing.
private float fallSpeed = 0.0f;
private float lastGroundedTime; //Last time since soldier was touching the ground.
private float lastLandingTime; //Last time since the soldier landed after a fall.
private	float targetForwardSpeed;
private	float targetStrafeSpeed;
private Char_crouch crouchControllerScript;
private bool  isFalling;
private Char_Health healthScript;
private float recoilAmount;
private float recoilAmountTarget;
//Vector3 moveDirection;
void  Start (){
	crouchControllerScript = GetComponent<Char_crouch>();
    healthScript = GetComponent<Char_Health>();
	isFalling = false;
	//soldier = transform.Find(soldierLocation);
}

void  Update (){
	//Stick to platforms.
    
    float rayHeight = 0.5f;
	Vector3 rayOrigin = transform.position + Vector3.up * rayHeight;
	Ray platformRay = new Ray(rayOrigin, Vector3.down);
	float rayDistance = rayHeight * 2.0f;
	RaycastHit platformHit;
	if (Physics.Raycast(platformRay, out platformHit, rayDistance)){
		if(platformHit.transform.root != transform){
			isGrounded = true;
            transform.position.y = platformHit.point.y;//ERROR Cannot modify the return value of 'UnityEngine.Transform.position' because it is not a variable
           
		}
	}
	else{
		isGrounded = false;
	}
	
	CharacterController controller = GetComponent<CharacterController>();
	//Hit recoil.
	Vector3 recoilVector = Vector3.zero;
	float recoilInhibit = 1.0f;
	float deathInhibit = 1.0f;
	if (healthScript != null){
		float lastHitTime = healthScript.GetLastHitTime();
		Vector3 recoilDirecion = healthScript.GetrecoilDirecion();
		float maxRecoil = 0.6f;
		float timeAfterHit = Time.time - lastHitTime;
		float recoilAmountTarget = 0.0f;
		if(timeAfterHit < maxRecoil && lastHitTime != 0){
			recoilAmountTarget = maxRecoil-timeAfterHit;
			recoilAmountTarget *= (1-Mathf.Abs(Input.GetAxis("Vertical")));
			recoilAmountTarget *= (1-Mathf.Abs(Input.GetAxis("Horizontal")));
		}
		if(recoilAmount < recoilAmountTarget){
			recoilAmount = Mathf.Lerp(recoilAmount, recoilAmountTarget, Time.deltaTime);
		}
		else{
			recoilAmount = Mathf.Lerp(recoilAmount, recoilAmountTarget, Time.deltaTime*20);
		}
		float biasRecoilAmount = recoilAmount; //Bias to 0.
		biasRecoilAmount /= maxRecoil;
		biasRecoilAmount = Mathf.Pow(biasRecoilAmount,2.0f);
		biasRecoilAmount *= maxRecoil;
		recoilVector = recoilDirecion * biasRecoilAmount;
		recoilInhibit = 1 -(recoilAmount*0.65f/maxRecoil);
		float health = healthScript.health;
		if(health <= 0){
			deathInhibit = 0;
		}
		else{
			deathInhibit = 1.0f;
		}
	}
	//Position.
	if (isGrounded){
		if (isFalling){
			isFalling = false;
			lastLandingTime = Time.time;
			stopAfterLanding = (lastLandingTime - lastGroundedTime) * 2.0f;
		}
		fallSpeed = 0.0f;
	}
	else{
		if (!isFalling){
			isFalling = true;
			lastGroundedTime = Time.time;
		}
		fallSpeed += gravity * Time.deltaTime;
	}
	Vector3 moveDirection;
	moveDirection.y -= fallSpeed;//ERROR Use of possibly unassigned field 'y'
	if (isGrounded){
		targetForwardSpeed = Input.GetAxis("Vertical");
		targetStrafeSpeed = Input.GetAxis("Horizontal");
		targetForwardSpeed *=  forwardSpeedMultiplier;
		targetStrafeSpeed *= strafeSpeedMultiplier;
		if(Input.GetAxis("Vertical") < 0){//Slow down going backwards;
			targetForwardSpeed *= 0.5f;
		}
		if(Input.GetKey(KeyCode.LeftShift)){//Sprint with left shift;
			targetForwardSpeed *= 1.5f;
			targetStrafeSpeed *= 1.5f;
		}
	}
	if(crouchControllerScript != null){ //Crouch speed multiplier.
		targetForwardSpeed = Mathf.Lerp(targetForwardSpeed, targetForwardSpeed * crouchControllerScript.crouchSpeedMultiplier, crouchControllerScript.globalCrouchBlend);
	}
	if(Time.time <= lastLandingTime + stopAfterLanding && stopAfterLanding > 0.5f){
		float timeSinceLanding = Time.time - lastLandingTime;
		float landingSpeedInhibit = Mathf.Pow(timeSinceLanding / stopAfterLanding, 1.5f);
		targetForwardSpeed *= landingSpeedInhibit;
		targetStrafeSpeed *= landingSpeedInhibit;
	}
	forwardSpeed = Mathf.Lerp(forwardSpeed, targetForwardSpeed, Time.deltaTime * 15.0f);
	strafeSpeed = Mathf.Lerp(strafeSpeed, targetStrafeSpeed, Time.deltaTime * 15.0f);
    moveDirection += soldier.forward * forwardSpeed * recoilInhibit * deathInhibit;//ERROR Use of unassigned local variable 'moveDirection'
	moveDirection += soldier.right * strafeSpeed * recoilInhibit * deathInhibit;
	moveDirection += recoilVector;
	controller.Move(moveDirection * Time.deltaTime);//Move the controller.
	//Rotation.
	float targetTurnSpeed = 0.0f;
	targetTurnSpeed= Input.GetAxis("Mouse X");
	targetTurnSpeed *= Mathf.Pow(turnSpeedMultiplier,3);
	targetTurnSpeed *= deathInhibit;
	turnSpeed = Mathf.Lerp(turnSpeed, targetTurnSpeed, Time.deltaTime * 25.0f);
	transform.rotation.eulerAngles.y += turnSpeed * Time.deltaTime;//ERROR Cannot modify the return value of 'UnityEngine.Quaternion.eulerAngles' because it is not a variable
    
}
}

http://files.m2h.nl//js_to_c.php

In JavaScript and UnityScript therein you don’t need to initialize variables like you do in C# so even if the conversion is correct you need to add a few lines in.

For ease of reading I will include the code which you had errors on here:


transform.position.y = platformHit.point.y; //ERROR Cannot modify the return value of 'UnityEngine.Transform.position' because it is not a variable

moveDirection.y -= fallSpeed;//ERROR Use of possibly unassigned field 'y'

moveDirection += soldier.forward * forwardSpeed * recoilInhibit *deathInhibit; //ERROR Use of unassigned local variable 'moveDirection'

transform.rotation.eulerAngles.y += turnSpeed * Time.deltaTime;//ERROR Cannot modify the return value of 'UnityEngine.Quaternion.eulerAngles' because it is not a variable

  • The Errors associated with
    moveDirection are coming up because
    “Vector3 moveDirection” is commented
    out at the top.

  • The other error is because in C# you need to read the whole Vector3 in as a Vector3.

eg:


            if (platformHit.transform.root != transform)
            {
                isGrounded = true;
                transform.position.y = platformHit.point.y;//ERROR Cannot modify the return value of 'UnityEngine.Transform.position' because it is not a variable

            }

becomes


            if (platformHit.transform.root != transform)
            {
                isGrounded = true;
                transform.position = new Vector3(transform.position.x, platformHit.point.y, transform.position.z);
            }

or


            if (platformHit.transform.root != transform)
            {
                isGrounded = true;
                Vector3 newpos = transform.position;
                newpos.y = platformHit.point.y;
                transform.position = newpos;        
            }

In laymans with C# don’t directly set transforms on objects unless via a Vector3 variable. Unity have done some work with UnityScript so you don’t need to know this. That is why the conversion breaks.