- Home /
The question is answered, right answer was accepted
Converting from javascript to c#
I used to use unity javascript and now I've decided to change to c#, so I started converting my scripts but I'm getting errors that I don't know how to fix them, can you help me? I will leave the scripts, in javascript and in c#(that is the one with errors) and I will make a list of the errors that are showing
Errors : Assets/Mine/Scripts/Parkour/PlayerMovementScript.cs(27,30): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected;
Assets/Mine/Scripts/Parkour/PlayerMovementScript.cs(33,19): error CS1612: Cannot modify a value type return value of UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable; Assets/Mine/Scripts/Parkour/PlayerMovementScript.cs(34,19): error CS1612: Cannot modify a value type return value of
UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable;
Assets/Mine/Scripts/Parkour/PlayerMovementScript.cs(39,34): error CS0246: The type or namespace name `vector3' could not be found. Are you missing a using directive or an assembly reference?;
script in unity javascript:
#pragma strict
var Movement : float;
var walkAcceleration : float = 1600;
var walkAccelAirRatio : float = 0.1;
var walkDeAcceleration : float = 0.3;
@HideInInspector
var walkDeAccelerationVolx : float;
@HideInInspector
var walkDeAccelerationVolz : float;
var cameraObject : GameObject;
var maxWalkSpeed : float = 10;
var horizontalMovement : Vector2;
var jumpVelocity : float = 240;
static var grounded : boolean = false;
var maxSlope : float = 60;
function Start () {
}
function Update ()
{
Movement = horizontalMovement.x + horizontalMovement.y;
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
if(grounded){
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeAccelerationVolx, walkDeAcceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeAccelerationVolz, walkDeAcceleration);}
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio* Time.deltaTime);
if (Input.GetButtonDown("Jump") && grounded)
rigidbody.AddRelativeForce(Vector3.up * jumpVelocity);
}
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
function OnCollisionExit ()
{
grounded = false;
}
script in c#:
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour {
public float Movement;
public float walkAcceleration = 1600f;
public float walkAccelAirRatio = 0.1f;
public float walkDeAcceleration = 0.3f;
[HideInInspector]
public float walkDeAccelerationVolx;
[HideInInspector]
public float walkDeAccelerationVolz;
public GameObject cameraObject;
public float maxWalkSpeed = 10f;
public Vector2 horizontalMovement;
public float jumpVelocity = 240f;
public static bool grounded = false;
public float maxSlope = 60f;
void Update (){
Movement = horizontalMovement.x + horizontalMovement.y / 2;
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
if(grounded){
float temp1x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeAccelerationVolx, walkDeAcceleration);
float temp2z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeAccelerationVolz, walkDeAcceleration);
rigidbody.velocity = new vector3(temp1x, rididbody.velocity.y,temp2z);}
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent<MouseLookScript>().currentYRotation, 0);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio* Time.deltaTime);
if (Input.GetButtonDown("Jump") && grounded)
rigidbody.AddRelativeForce(Vector3.up * jumpVelocity);
}
void OnCollisionStay ( Collision collision ){
foreach(ContactPoint contact in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true; } } void OnCollisionExit (){ grounded = false; } }
thanks in advance.
that's what I used first to convert, fixed some of the errors but can't fix some of them (these 4 I wrote)
ok do you know how to write in C#? if you do
than do a slow process rewriting the code it's that simple
I know I've once struggled in my code tried to fix the bugs not errors for 1 week and after that I aggro on my self commented whole script around 700 lines rewrite it made it better more efficient easier to read in only 1 day
so my recommendation is you start commenting your new script and rewriting it what's missing
No, I think if I knew how to write in c# I could fix my bugs, I kinda know how to write in c# but at the same time I think it wouln't go well since that script is kinda complex for one of the first scripts I'm trying to work with in c#
Here's some links I found useful in converting between C# and JS :
Answer by Nidre · Apr 08, 2013 at 06:17 AM
Havent tried in compiler but it should work.I have commented out the parts you have mistaken so you can see what i have changed. hope this helps.
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour {
public float Movement;
public float walkAcceleration = 1600f;
public float walkAccelAirRatio = 0.1f;
public float walkDeAcceleration = 0.3f;
[HideInInspector]
public float walkDeAccelerationVolx;
[HideInInspector]
public float walkDeAccelerationVolz;
public GameObject cameraObject;
public float maxWalkSpeed = 10f;
public Vector2 horizontalMovement;
public float jumpVelocity = 240f;
public static bool grounded = false;
public float maxSlope = 60f;
void Update (){
Movement = horizontalMovement.x + horizontalMovement.y / 2;
//horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
horizontalMovement = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity = new Vector3(horizontalMovement.x,horizontalMovement.y,rigidbody.velocity.z);
//rigidbody.velocity.x = horizontalMovement.x;
//rigidbody.velocity.z = horizontalMovement.y;
if(grounded){
float temp1x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeAccelerationVolx, walkDeAcceleration);
float temp2z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeAccelerationVolz, walkDeAcceleration);
rigidbody.velocity = new Vector3(temp1x, rididbody.velocity.y,temp2z);}
//rigidbody.velocity = new vector3(temp1x, rididbody.velocity.y,temp2z);}
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent<MouseLookScript>().currentYRotation, 0);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio* Time.deltaTime);
if (Input.GetButtonDown("Jump") && grounded)
rigidbody.AddRelativeForce(Vector3.up * jumpVelocity);
}
thanks, no more errors, but I'm getting a awkard bug, when I move positively in the global z axis, I start moving upwards, if you could test the code yourself and see what is wrong, create a capsule, attach this script to it and create a camera object, and attach this script to it:
using System.Collections;
public class $$anonymous$$ouseLookScript : $$anonymous$$onoBehaviour {
public float lookSensivity = 5f;
[HideInInspector]
public float yRotation;
[HideInInspector]
public float xRotation;
[HideInInspector]
public float currentYRotation;
[HideInInspector]
public float currentXRotation;
[HideInInspector]
public float yRotationV;
[HideInInspector]
public float xRotationV;
public float lookSmoothDamp = 0.1f;
public bool isArcade = false;
public bool isTrial = false;
void Start (){
Screen.lockCursor = true;
}
void Update (){
yRotation += Input.GetAxis("$$anonymous$$ouse X") * lookSensivity;
xRotation -= Input.GetAxis("$$anonymous$$ouse Y") * lookSensivity;
xRotation = $$anonymous$$athf.Clamp(xRotation, -90, 90);
currentXRotation = $$anonymous$$athf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
currentYRotation = $$anonymous$$athf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
if (Input.GetButtonDown("Esc"))
Screen.lockCursor = false;
if (Input.GetButtonDown("Fire1"))
Screen.lockCursor = true;
if (isArcade){
yRotation = $$anonymous$$athf.Clamp (yRotation, -80, 80);}
}
}
well which axis is up and which axises are sides are up to you camera angle.Normally y is used for up and down.but if you have used a different camera angle it would act like sides etc.
Btw you can open this question seperately and accept the current one if helped ;) that way you can get better help for that problem ;):)
ok I see, so I will accept this question, probably give my things as a unity package in another question maybe someone can fix this. thanks anyway
done, I was in hurry when I said that because I had to leave for school
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pairing and GUITexture with a Timer? 1 Answer
How do i fix "Script does not exist" error when trying to add my script to something 3 Answers
About "translating" js into C# 1 Answer