Can someone Convert 2 JScripts into C#

Can Someone Convert these two scripts into C#, I’m not good with JavaScript…
So could someone give me a helping hand.?

private var jumpSpeed:float = 18.0;
private var gravity:float = 32.0;
private var runSpeed:float = 15.0;
private var walkSpeed:float = 45.0;
private var rotateSpeed:float = 150.0;
 
private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
private var isWalking:boolean = false;
private var moveStatus:String = "idle";
var trigger : GameObject; 
static var dead : boolean = false;
private var attack;  
var attackPosition : Transform; 

function Start () 
{ 
	
}
function delay () 
{ 
	yield WaitForSeconds(5);
}
function Update ()
{
	
        if(dead == false) {
       
			if(Input.GetKeyDown("f")) 
			{   
				attack= true;   
				moveStatus = "attack"; 
				animation.Play("punch", PlayMode.StopAll);
				//animation.Play("upperjab", PlayMode.StopAll);   
				 delay();
				
			//	Instantiate(trigger, attackPosition.position, attackPosition.rotation); 
				
			} 
		
       
 
 
        // Only allow movement and jumps while grounded
        if(grounded) {
			if(Input.GetKey(KeyCode.A)  (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S)) ) 
				{  
					animation.CrossFade("strafeLeft", 0.2);
					//animation.Play("strafeLeft", PlayMode.StopAll);
				} 
			if(Input.GetKey(KeyCode.D)  (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S)) ) 
	   {  
			animation.CrossFade("strafeRight", 0.2);
			//animation.Play("strafeRight", PlayMode.StopAll);
	   }
				if(moveStatus== "idle") 
				{ 
					animation.Play("idle"); 
				}
			 
                moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
               
                // if moving forward and to the side at the same time, compensate for distance
                // TODO: may be better way to do this?
                if(Input.GetMouseButton(1)  Input.GetAxis("Horizontal")  Input.GetAxis("Vertical")) {
                        moveDirection *= .7;
					
                       
                }
               
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection *= isWalking ? walkSpeed : runSpeed;
               
                moveStatus = "idle";
                if(moveDirection != Vector3.zero) {
                        moveStatus = isWalking ? "walking" : "running";
							
						}
               
			  if( Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S)){
			  
				animation.CrossFade("run", 0.2);
			   }
                // Jump!
                //if(Input.GetButton("Jump"))
               
                if (Input.GetKeyDown(KeyCode.Space)) {
              animation.Play("jump", PlayMode.StopAll);
                        moveDirection.y = jumpSpeed; 
						
						}
        }
       
        // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
        if(Input.GetMouseButton(1)) {
                transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
        } else {
                transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
               
        }
      
	   
	   
       
               
               
               
       
        // Toggle walking/running with the T key
        //if(Input.GetKeyDown("t"))
                //isWalking = !isWalking;
 
 
       
       
        //Apply gravity
        moveDirection.y -= gravity * Time.deltaTime;
 
       
        //Move controller
        var controller:CharacterController = GetComponent(CharacterController);
        var flags = controller.Move(moveDirection * Time.deltaTime);
        grounded = (flags  CollisionFlags.Below) != 0;
       
 
        }
       
       
        if(Input.GetMouseButton(1) || Input.GetMouseButton(0)) {
                //Screen.lockCursor = true;
               
                //Screen.showCursor = false;
               
               
                //var mouse1 = Input.mousePosition.y;
                //var mouse2 = Input.mousePosition.x;
               
                }
               
                //Vector3 mousePos = Input.mousePosition;
        else  {
                //Screen.lockCursor = false;
                //Screen.showCursor = false;
               
                //Input.mousePosition.y = mouse1;
                //Input.mousePosition.x = mouse2;
               
                //Input.mousePosition = mousePos;
               
                }
       
       
}
 
//@script RequireComponent(CharacterController)
var target:Transform;                                                   // Target to follow
var targetHeight = 1.7;                                                 // Vertical offset adjustment
var distance = 12.0;                                                    // Default Distance
var offsetFromWall = 0.1;                                               // Bring camera away from any colliding objects
var maxDistance = 20;                                           // Maximum zoom Distance
var minDistance = 0.6;                                          // Minimum zoom Distance
var xSpeed = 200.0;                                                     // Orbit speed (Left/Right)
var ySpeed = 200.0;                                                     // Orbit speed (Up/Down)
var yMinLimit = -80;                                                    // Looking up limit
var yMaxLimit = 80;                                                     // Looking down limit
var zoomRate = 40;                                                      // Zoom Speed
var rotationDampening = 3.0;                            // Auto Rotation speed (higher = faster)
var zoomDampening = 5.0;                                        // Auto Zoom speed (Higher = faster)
var collisionLayers:LayerMask = -1;             // What the camera will collide with
var lockToRearOfTarget = false;                         // Lock camera to rear of target
var allowMouseInputX = true;                            // Allow player to control camera angle on the X axis (Left/Right)
var allowMouseInputY = true;                            // Allow player to control camera angle on the Y axis (Up/Down)
 
private var xDeg = 0.0;
private var yDeg = 0.0;
private var currentDistance;
private var desiredDistance;
private var correctedDistance;
private var rotateBehind = false;
 
 
@script AddComponentMenu("Camera-Control/Third Person Camera Orbit (MMORPG Like)")
 
function Start ()
{
        var angles:Vector3 = transform.eulerAngles;
        xDeg = angles.x;
        yDeg = angles.y;
        currentDistance = distance;
        desiredDistance = distance;
        correctedDistance = distance;
       
        // Make the rigid body not change rotation
        if (rigidbody)
                rigidbody.freezeRotation = true;
               
        if (lockToRearOfTarget)
                rotateBehind = true;
 }
   
//Only Move camera after everything else has been updated
function LateUpdate ()
{
 
 
 
        // Don't do anything if target is not defined
        if (!target)
                return;
       
        var vTargetOffset:Vector3;
       
         
        // If either mouse buttons are down, let the mouse govern camera position
        if (GUIUtility.hotControl == 0)
        {
                if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
                {
                        //Check to see if mouse input is allowed on the axis
                        if (allowMouseInputX)
                                xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02;
                        else
                                RotateBehindTarget();
                        if (allowMouseInputY)
                                yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02;
                       
                        //Interrupt rotating behind if mouse wants to control rotation
                        if (!lockToRearOfTarget)
                                rotateBehind = false;
                }
               
        // otherwise, ease behind the target if any of the directional keys are pressed
                else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0 || rotateBehind)
                {
                        //RotateBehindTarget();
                }
        }
        yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit);
 
        // Set camera rotation
        var rotation:Quaternion = Quaternion.Euler (yDeg, xDeg, 0);
 
        // Calculate the desired distance
        desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
        desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);
        correctedDistance = desiredDistance;
 
        // Calculate desired camera position
        vTargetOffset = Vector3 (0, -targetHeight, 0);
        var position:Vector3 = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
 
        // Check for collision using the true target's desired registration point as set by user using height
        var collisionHit:RaycastHit;
        var trueTargetPosition:Vector3 = Vector3 (target.position.x, target.position.y + targetHeight, target.position.z);
 
        // If there was a collision, correct the camera position and calculate the corrected distance
        var isCorrected = false;
        if (Physics.Linecast (trueTargetPosition, position, collisionHit, collisionLayers))
        {
                // Calculate the distance from the original estimated position to the collision location,
                // subtracting out a safety "offset" distance from the object we hit.  The offset will help
                // keep the camera from being right on top of the surface we hit, which usually shows up as
                // the surface geometry getting partially clipped by the camera's front clipping plane.
                correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall;
                isCorrected = true;
        }
 
        // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
        currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
 
        // Keep within limits
        currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance);
 
        // Recalculate position based on the new currentDistance
        position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
       
        //Finally Set rotation and position of camera
        transform.rotation = rotation;
        transform.position = position;
}
 
function RotateBehindTarget()
{
        var targetRotationAngle:float = target.eulerAngles.y;
        var currentRotationAngle:float = transform.eulerAngles.y;
        xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
       
        // Stop rotating behind if not completed
        if (targetRotationAngle == currentRotationAngle)
        {
                if (!lockToRearOfTarget)
                        rotateBehind = false;
        }
        else
                rotateBehind = true;
 
}
 
 
static function ClampAngle (angle : float, min : float, max : float)
{
   if (angle < -360)
      angle += 360;
   if (angle > 360)
      angle -= 360;
   return Mathf.Clamp (angle, min, max);
}

Thank you :slight_smile:

1523079–87329–$Camera.js (6.9 KB)
1523079–87330–$MouseMovement.js (4.44 KB)

You should be able to do this yourself quite easily really with a little reading. It will also help in the long run to at least make an effort in trying to convert the files rather then rely on other people.

The main differences are declaring variables:

private var jumpSpeed:float = 18.0;
private float jumpSpeed = 18.0f;

Declaring functions

function Start ()
void Start ()
  • void is the return type. Void means return nothing, int/float/string/whatever means you have to return that from the function.
    The general code itself should not need to much reworking really as the basic operators =/-/+/ect… and built-in functions in unity should transfer from JScript to Unity C# quite easily.

Okay, thank you.

I’ve tryed doing this myself, all i get is thousand and one different errors in the script after I change it all.

But thank you very much!.

try this

Have and it doesn’t do anything.

Assuming your adding the correct using/includes:

using UnityEngine;
using System.Collections;

and your also declaring the class? I think the class and C# file name have to be the same or Unity seems to throw errors.

public class MYCLASSNAME : MonoBehaviour

put up the ones you tried, I can try and correct them. just don’t want to go through the tedious job of changing all the variables…

using UnityEngine;
using System.Collections;

public class Camera : MonoBehaviour {
Transform target;                                                   // Target to follow

public float targetHeight= 1.7f;                                                 // Vertical offset adjustment

public float distance= 12.0f;                                                    // Default Distance

public float offsetFromWall= 0.1f;                                               // Bring camera away from any colliding objects

public float maxDistance= 20;                                           // Maximum zoom Distance

public float minDistance= 0.6f;                                          // Minimum zoom Distance

public float xSpeed= 200.0f;                                                     // Orbit speed (Left/Right)

public float ySpeed= 200.0f;                                                     // Orbit speed (Up/Down)

public float yMinLimit= -80;                                                    // Looking up limit

public float yMaxLimit= 80;                                                     // Looking down limit

public float zoomRate= 40;                                                      // Zoom Speed

public float rotationDampening= 3.0f;                            // Auto Rotation speed (higher = faster)

public float zoomDampening= 5.0f;                                        // Auto Zoom speed (Higher = faster)

LayerMask collisionLayers = -1;             // What the camera will collide with

public bool lockToRearOfTarget= false;                         // Lock camera to rear of target

public bool allowMouseInputX= true;                            // Allow player to control camera angle on the X axis (Left/Right)

public bool allowMouseInputY= true;                            // Allow player to control camera angle on the Y axis (Up/Down)

 

private float xDeg= 0.0f;

private float yDeg= 0.0f;

private int currentDistance;

private int desiredDistance;

private int correctedDistance;

private bool rotateBehind= false;

 

 

//@script AddComponentMenu("Camera-Control/Third Person Camera Orbit (MMORPG Like)")

 

void  Start (){

        Vector3 angles = transform.eulerAngles;

        xDeg = angles.x;

        yDeg = angles.y;

        currentDistance = distance;

        desiredDistance = distance;

        correctedDistance = distance;

       

        // Make the rigid body not change rotation

        if (rigidbody)

                rigidbody.freezeRotation = true;

               

        if (lockToRearOfTarget)

                rotateBehind = true;

 }

   

//Only Move camera after everything else has been updated

void  LateUpdate (){

 

 

 

        // Don't do anything if target is not defined

        if (!target)

                return;

       

        Vector3 vTargetOffset;

       

         

        // If either mouse buttons are down, let the mouse govern camera position

        if (GUIUtility.hotControl == 0)

        {

                if (Input.GetMouseButton(0) || Input.GetMouseButton(1))

                {

                        //Check to see if mouse input is allowed on the axis

                        if (allowMouseInputX)

                                xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;

                        else

                                RotateBehindTarget();

                        if (allowMouseInputY)

                                yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;

                       

                        //Interrupt rotating behind if mouse wants to control rotation

                        if (!lockToRearOfTarget)

                                rotateBehind = false;

                }

               

        // otherwise, ease behind the target if any of the directional keys are pressed

                else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0 || rotateBehind)

                {

                        //RotateBehindTarget();

                }

        }

        yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit);

 

        // Set camera rotation

        Quaternion rotation = Quaternion.Euler (yDeg, xDeg, 0);

 

        // Calculate the desired distance

        desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);

        desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);

        correctedDistance = desiredDistance;

 

        // Calculate desired camera position

        vTargetOffset = Vector3 (0, -targetHeight, 0);

        Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);

 

        // Check for collision using the true target's desired registration point as set by user using height

        RaycastHit collisionHit;

        Vector3 trueTargetPosition = Vector3 (target.position.x, target.position.y + targetHeight, target.position.z);

 

        // If there was a collision, correct the camera position and calculate the corrected distance

        bool isCorrected= false;

        if (Physics.Linecast (trueTargetPosition, position, collisionHit, collisionLayers))

        {

                // Calculate the distance from the original estimated position to the collision location,

                // subtracting out a safety "offset" distance from the object we hit.  The offset will help

                // keep the camera from being right on top of the surface we hit, which usually shows up as

                // the surface geometry getting partially clipped by the camera's front clipping plane.

                correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall;

                isCorrected = true;

        }

 

        // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance

        currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;

 

        // Keep within limits

        currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance);

 

        // Recalculate position based on the new currentDistance

        position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);

       

        //Finally Set rotation and position of camera

        transform.rotation = rotation;

        transform.position = position;

}

 

void  RotateBehindTarget (){

        float targetRotationAngle = target.eulerAngles.y;

        float currentRotationAngle = transform.eulerAngles.y;

        xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);

       

        // Stop rotating behind if not completed

        if (targetRotationAngle == currentRotationAngle)

        {

                if (!lockToRearOfTarget)

                        rotateBehind = false;

        }

        else

                rotateBehind = true;

 

}

 

 

static void  ClampAngle ( float angle ,   float min ,   float max  ){

   if (angle < -360)

      angle += 360;

   if (angle > 360)

      angle -= 360;

   return Mathf.Clamp (angle, min, max);

}
}

1523214–87335–$Camera.cs (7.5 KB)

there wasn’t that much wrong, small things like if you make a Vector3 in C# you need the new keyword, if you do a raycast, the RayCastHit needs the out keyword.
and some errors i commented what and why i changed things.
enjoy…

1523239–87336–$MyCamera.cs (7.62 KB)

Thank you soo much…!