- Home /
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
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 Advanced$$anonymous$$ovement 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 =]
I have a player input script set up and it sends messages to the movement script EX: if(Input.GetButtonUp("$$anonymous$$ove Forward")) { Send$$anonymous$$essage("$$anonymous$$ove$$anonymous$$eForward", Advanced$$anonymous$$ovement.Forward.none); }
if(Input.GetButton("Rotate Player")) {
if(Input.GetAxis("Rotate Player") > 0) {
Send$$anonymous$$essage("Rotate$$anonymous$$e", Advanced$$anonymous$$ovement.Turn.right);
}
else {
Send$$anonymous$$essage("Rotate$$anonymous$$e", Advanced$$anonymous$$ovement.Turn.left);
}
}
I was planning on changing it up so that: if (movement.z > 0){ Advanced$$anonymous$$ovement.Send$$anonymous$$essage("$$anonymous$$ove$$anonymous$$eForward", Advanced$$anonymous$$ovement.Forward.forward);
so the player moves. Using a finacestate machine, can you send that type of message from java to c#?
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
So how would i set it up? i have my input script in c# :if(Input.GetButtonUp("$$anonymous$$ove Forward")) { Send$$anonymous$$essage("$$anonymous$$ove$$anonymous$$eForward", Advanced$$anonymous$$ovement.Forward.none); } Then that sends a message to another c# script
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;
}
}
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
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Particle circle HELP 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers