• 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 /
avatar image
Question by Babilin · Jun 29, 2012 at 05:57 AM · c#convertc

Convert to C#,I don't know how to convert this

I tried throughout the whole day and i really don't know how to make this C# the name of the script is CameraRelatveControl.js

        // stick is used to rotate the camera around the character.
 // A quick double-tap on the right joystick will make the 
 // character jump. 
 //////////////////////////////////////////////////////////////
 
 #pragma strict
 
 // This script must be attached to a GameObject that has a CharacterController
 @script RequireComponent( CharacterController )
 
 var moveJoystick : Joystick;
 var rotateJoystick : Joystick;
 
 var cameraPivot : Transform;                        // The transform used for camera rotation
 var cameraTransform : Transform;                    // The actual transform of the camera
 
 var speed : float = 5;                                // Ground speed                
 var rotationSpeed : Vector2 = Vector2( 50, 25 );    // Camera rotation speed for each axis
 
 
 
 
 
 
 
 
 function OnEndGame()
 {
     // Disable joystick when the game ends    
     moveJoystick.Disable();
     rotateJoystick.Disable();
     
     // Don't allow any more control changes when the game ends
     this.enabled = false;
 }
 
 function Update()
 {
 
     var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
         // We only want the camera-space horizontal direction
     movement.y = 0;
     movement.Normalize(); // Adjust magnitude after ignoring vertical movement
     
     // Let's use the largest component of the joystick position for the speed.
     var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
     movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
     
     
         
     
     if(movement.x > 0){
     print("right");
     
     }
     
     if(movement.x < 0){
     print("left");
     
     }
     
         if(movement.z > 0){
                 print("up");
                 
             }
             
     if(movement.z < 0){
     print("backward");
     
     }
     
     
     // Scale joystick input with rotation speed
     var camRotation = rotateJoystick.position;
     camRotation.x *= rotationSpeed.x;
     camRotation.y *= rotationSpeed.y;
     camRotation *= Time.deltaTime;
     
     // Rotate around the character horizontally in world, but use local space
     // for vertical rotation
     cameraPivot.Rotate( 0, camRotation.x, 0, Space.World );
     cameraPivot.Rotate( -camRotation.y, 0, 0 );
 }

please help

Comment

People who like this

0 Show 4
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 AlucardJay · Jun 29, 2012 at 06:08 AM 0
Share

Just curious, why do you need this is C# (esp if you're having problems translating it), and is this the original JS script you're trying to convert (i.e AdvancedMovement is not declared in this script)?

I can possibly help, but please edit the question to show your script, then the original script you are converting =]

avatar image Babilin · Jun 29, 2012 at 06:14 AM 0
Share

I have a player input script set up and it sends messages to the movement script EX: if(Input.GetButtonUp("Move Forward")) { SendMessage("MoveMeForward", AdvancedMovement.Forward.none); }

     if(Input.GetButton("Rotate Player")) {
         if(Input.GetAxis("Rotate Player") > 0) {
             SendMessage("RotateMe", AdvancedMovement.Turn.right);
         }
         else {
             SendMessage("RotateMe", AdvancedMovement.Turn.left);
         }
     }

I was planning on changing it up so that: if (movement.z > 0){ AdvancedMovement.SendMessage("MoveMeForward", AdvancedMovement.Forward.forward);

so the player moves. Using a finacestate machine, can you send that type of message from java to c#?

avatar image AlucardJay · Jun 29, 2012 at 06:21 AM 0
Share

it is possible to use both languages, but the way they compile means that they only work one way i.e. C# gets compiled before JS, JS can see C# but C# cannot see JS :

http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html => point 3

http://answers.unity3d.com/questions/208383/referencing-a-c-component-script-from-js-script.html

http://answers.unity3d.com/questions/243112/calling-c-classes-from-js.html

sry, i edited my above comment while you posted, can you edit the question to show your script, then the original script you are converting, thx

avatar image Babilin · Jun 29, 2012 at 06:29 AM 0
Share

So how would i set it up? i have my input script in c# :if(Input.GetButtonUp("Move Forward")) { SendMessage("MoveMeForward", AdvancedMovement.Forward.none); } Then that sends a message to another c# script

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by AlucardJay · Jun 29, 2012 at 06:54 AM

Without the other classes I cannot test this, but something like this (again, untested) :

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (CharacterController))]
 
 public class CameraRelativeControl : MonoBehaviour 
 {
 
     Joystick moveJoystick;
     Joystick rotateJoystick;
     
     Transform cameraPivot;                         // The transform used for camera rotation
     Transform cameraTransform;                     // The actual transform of the camera
     
     float speed = 5.0f;                         // Ground speed          
     Vector2 rotationSpeed = new Vector2( 50, 25 );     // Camera rotation speed for each axis
     
     
     void Update()
     {
         Vector3 movement = cameraTransform.TransformDirection( new Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
         
         // We only want the camera-space horizontal direction
         movement.y = 0;
         movement.Normalize(); // Adjust magnitude after ignoring vertical movement
     
         // Let's use the largest component of the joystick position for the speed.
         Vector2 absJoyPos = new Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
         
         // * not sure about this line *
         movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
     
     
         if(movement.x > 0){
             print("right");
         }
     
         if(movement.x < 0){
         print("left");
         }
     
         if(movement.z > 0){
             print("up");
         }
     
         if(movement.z < 0){
             print("backward");
         }
     
     
         // Scale joystick input with rotation speed
         var camRotation = rotateJoystick.position;
         camRotation.x *= rotationSpeed.x;
         camRotation.y *= rotationSpeed.y;
         camRotation *= Time.deltaTime;
     
         // Rotate around the character horizontally in world, but use local space
         // for vertical rotation
         cameraPivot.Rotate( 0, camRotation.x, 0, Space.World );
         cameraPivot.Rotate( -camRotation.y, 0, 0 );
     }
     
     
     void OnEndGame()
     {
         // Disable joystick when the game ends 
         moveJoystick.Disable();
         rotateJoystick.Disable();
     
         // Don't allow any more control changes when the game ends
         this.enabled = false;
     }
 }

Comment
Berenger

People who like this

1 Show 1 · 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 AlucardJay · Jun 30, 2012 at 03:40 AM 0
Share

Here's some links I found useful in converting between C# / JS that I forgot to add =]

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_Kind_Of_Array_Or_Collection_Should_I_Use?

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

JS to C# conversion is a complete disaster 1 Answer

Particle circle HELP 1 Answer

Distribute terrain in zones 3 Answers


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