Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 22, 2013 at 03:56 PM by MadJohny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MadJohny · Apr 07, 2013 at 07:57 PM · c#javascript

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.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Apr 07, 2013 at 08:00 PM 0
Share

does this help?

Converter

avatar image MadJohny · Apr 07, 2013 at 08:05 PM 0
Share

that's what I used first to convert, fixed some of the errors but can't fix some of them (these 4 I wrote)

avatar image sdgd · Apr 07, 2013 at 08:12 PM 0
Share

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

avatar image MadJohny · Apr 07, 2013 at 08:14 PM 0
Share

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#

avatar image AlucardJay · Apr 07, 2013 at 08:16 PM 1
Share

Here's some links I found useful in converting between C# and JS :

  • http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

  • http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

  • http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/

Show more comments

1 Reply

  • Sort: 
avatar image
2
Best Answer

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);
  
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadJohny · Apr 08, 2013 at 04:07 PM 0
Share

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);}
 }
 }
avatar image Nidre · Apr 08, 2013 at 09:42 PM 0
Share

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 ;):)

avatar image MadJohny · Apr 09, 2013 at 08:13 AM 0
Share

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

avatar image Nidre · Apr 09, 2013 at 08:34 AM 0
Share

still no accept :(:)

avatar image MadJohny · Apr 09, 2013 at 04:37 PM 0
Share

done, I was in hurry when I said that because I had to leave for school

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges