Help in translate Java to C

Hi there I have this code in java:


@script RequireComponent( CharacterController )

static var POS : int;
public var useAccelerometer : boolean = true;
public var useTouch : boolean = true;
public var useJoypad : boolean = true;
private var player1 : Transform;
private var touchVelocity : float;
var touchSpeed : float = 0.3;
var accelerometerSpeed : float = 30;
private var accelAxis : int = 0;
private var touchAxis : int = 0;
var horizontalOrientation : boolean = true;
private var accelCalib : Vector3 = Vector3.zero;
private var smooth : float;
var cam : Camera;
var speed = 30.0;

// joystick controller variables
var character : CharacterController;
var thisTransform : Transform;
var moveJoystick : Joypad;
var forwardSpeed : float = 2;
var backwardSpeed : float = 2;
var sidestepSpeed : float = 2;
private var velocity : Vector3;
public var JoystickPrefab:GameObject;

function Start()
{

cam = gameObject.FindWithTag(“MainCamera”).GetComponent(“Camera”);
cam = gameObject.FindWithTag(“MainCamera”).GetComponent(“Camera”);
player1 = transform;

iPhoneKeyboard.autorotateToPortrait = false;
iPhoneKeyboard.autorotateToPortraitUpsideDown = false;
iPhoneKeyboard.autorotateToLandscapeRight = false;
iPhoneKeyboard.autorotateToLandscapeLeft = true;

//joystick
character = GetComponent( CharacterController );
thisTransform = transform;

if (useJoypad)
{
var joypos : Vector3 = Vector3(0,0, 0 );
Instantiate(JoystickPrefab, joypos, Quaternion.identity);
}

}

private var targetPosition:Vector3;

function Update () {

var movement = thisTransform.TransformDirection( Vector3( moveJoystick.position.x, moveJoystick.position.y, 0 ) );

// We only want horizontal movement
//movement.y = 0;
movement.Normalize();

// Horizontal
if (transform.position.x <= -6.5f)
transform.position = new Vector3(-6.5f, transform.position.y, transform.position.z);
else if (transform.position.x >= 6.5f)
transform.position = new Vector3(6.5f, transform.position.y, transform.position.z);

// Vertical
if (transform.position.y <= -4.0f)
transform.position = new Vector3(transform.position.x, -4.0f, transform.position.z);
else if (transform.position.y >= 4.0f)
transform.position = new Vector3(transform.position.x, 4.0f, transform.position.z);

//iphone accelerometer START
if (useAccelerometer)
{
var dir : Vector3 = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand

// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
if (iPhoneSettings.screenOrientation == iPhoneScreenOrientation.LandscapeLeft)
{

dir.x = -Input.acceleration.y;
dir.y = Input.acceleration.x;
}
else if (iPhoneSettings.screenOrientation == iPhoneScreenOrientation.LandscapeRight)
{
dir.x = Input.acceleration.y;
dir.y = -Input.acceleration.x;
}

// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();

// Make it move 10 meters per second instead of 10 meters per frame…
dir *= Time.deltaTime;

// Move object
transform.Translate (dir * speed);
}//iphone accelerometer END

//iphone touch/mouse START
if (useTouch)
{

for (var touch : iPhoneTouch in iPhoneInput.touches)
{
var p : Vector3 = cam.ScreenToWorldPoint (Vector3 (touch.position.x,touch.position.y,0));

transform.position = new Vector3(p.x, p.y, 0);

}

}//iphone touch/mouse END

//iphone joypad START

if (useJoypad)
{

var characterVelocity = character.velocity;
var horizontalVelocity : Vector3 = characterVelocity;
//horizontalVelocity.y = 0;
var speed = horizontalVelocity.magnitude;
var upwardsMotion = Vector3.Dot( thisTransform.up, characterVelocity );

var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ),Mathf.Abs( moveJoystick.position.y ) );
if ( absJoyPos.y > absJoyPos.x )
{
if ( moveJoystick.position.y > 0 )

movement *= forwardSpeed * absJoyPos.y;
else
{
movement *= backwardSpeed * absJoyPos.y;
}
}
else
{
movement *= sidestepSpeed * absJoyPos.x;
}

// Actually move the character
character.Move( movement );
}//iphone accelerometer END

if ((iPhoneInput.orientation == iPhoneOrientation.LandscapeLeft) (iPhoneSettings.screenOrientation != iPhoneScreenOrientation.LandscapeLeft))
{
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeLeft;
}

if ((iPhoneInput.orientation == iPhoneOrientation.LandscapeRight) (iPhoneSettings.screenOrientation != iPhoneScreenOrientation.LandscapeRight))
{
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeRight;
}

}


Continue…

Continue…

And I translated the code in C sharp like this with no errors:


using UnityEngine;
using System.Collections;

[RequireComponent (typeof (CharacterController))]
[RequireComponent (typeof (Joystick))]
public class GameControllerC : MonoBehaviour
{

static int POS;
public bool useAccelerometer = true;
public bool useTouch = true;
public bool useJoypad = true;
private Transform player1;
private float touchVelocity;
public float touchSpeed = 0.3F;
public float accelerometerSpeed = 30;
private int accelAxis = 0;
private int touchAxis = 0;
public bool horizontalOrientation = true;
private Vector3 accelCalib = Vector3.zero;
private float smooth;
public Camera cam;
public float speed = 30.0F;

// joystick controller variables
public CharacterController character;
public Transform thisTransform;
public Joystick moveJoystick;
public float forwardSpeed = 2;
public float backwardSpeed = 2;
public float sidestepSpeed = 2;
private Vector3 velocity;
public GameObject JoystickPrefab;

// Use this for initialization
void Start ()
{
//cam = gameObject.FindWithTag(“MainCamera”).GetComponent(“Camera”);
//cam = gameObject.FindWithTag(“MainCamera”).GetComponent(“Camera”);
//GameObject cam = GameObject.FindWithTag(“MainCamera”).gameObject.GetComponent(“Camera”);
Camera cam = (Camera)gameObject.GetComponent(“Camera”);
//cam = (Joystick[ ]) FindObjectsOfType( typeof(Joystick) );
player1 = transform;

iPhoneKeyboard.autorotateToPortrait = false;
iPhoneKeyboard.autorotateToPortraitUpsideDown = false;
iPhoneKeyboard.autorotateToLandscapeRight = false;
iPhoneKeyboard.autorotateToLandscapeLeft = true;

//joystick

CharacterController character = gameObject.GetComponent();
thisTransform = transform;

if (useJoypad)
{
Vector3 joypos = new Vector3(0,0, 0 );
Instantiate(JoystickPrefab, joypos, Quaternion.identity);
}

}

private Vector3 targetPosition;

// Update is called once per frame
void Update ()
{

Vector3 movement = thisTransform.TransformDirection( new Vector3( moveJoystick.position.x, moveJoystick.position.y, 0 ) );

// We only want horizontal movement
//movement.y = 0;
movement.Normalize();

// Horizontal
if (transform.position.x <= -6.5f)
transform.position = new Vector3(-6.5f, transform.position.y, transform.position.z);
else if (transform.position.x >= 6.5f)
transform.position = new Vector3(6.5f, transform.position.y, transform.position.z);

// Vertical
if (transform.position.y <= -4.0f)
transform.position = new Vector3(transform.position.x, -4.0f, transform.position.z);
else if (transform.position.y >= 4.0f)
transform.position = new Vector3(transform.position.x, 4.0f, transform.position.z);

//iphone accelerometer START
if (useAccelerometer)
{
Vector3 dir = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand

// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
if (iPhoneSettings.screenOrientation == iPhoneScreenOrientation.LandscapeLeft)
{

dir.x = -Input.acceleration.y;
dir.y = Input.acceleration.x;
}
else if (iPhoneSettings.screenOrientation == iPhoneScreenOrientation.LandscapeRight)
{
dir.x = Input.acceleration.y;
dir.y = -Input.acceleration.x;
}

// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();

// Make it move 10 meters per second instead of 10 meters per frame…
dir *= Time.deltaTime;

// Move object
transform.Translate (dir * speed);
}//iphone accelerometer END

//iphone touch/mouse START
if (useTouch)
{

foreach (Touch touch in Input.touches)
{
Vector3 p = cam.ScreenToWorldPoint (new Vector3 (touch.position.x,touch.position.y,0));

transform.position = new Vector3(p.x, p.y, 0);

}

}//iphone touch/mouse END

//iphone joypad START

if (useJoypad)
{

var characterVelocity = character.velocity;
Vector3 horizontalVelocity = characterVelocity;
//horizontalVelocity.y = 0;
var speed = horizontalVelocity.magnitude;
var upwardsMotion = Vector3.Dot( thisTransform.up, characterVelocity );

var absJoyPos = new Vector2( Mathf.Abs( moveJoystick.position.x ),Mathf.Abs( moveJoystick.position.y ) );
if ( absJoyPos.y > absJoyPos.x )
{
if ( moveJoystick.position.y > 0 )

movement *= forwardSpeed * absJoyPos.y;
else
{
movement *= backwardSpeed * absJoyPos.y;
}
}
else
{
movement *= sidestepSpeed * absJoyPos.x;
}

// Actually move the character
character.Move( movement );
}//iphone accelerometer END

if ((iPhoneInput.orientation == iPhoneOrientation.LandscapeLeft) (iPhoneSettings.screenOrientation != iPhoneScreenOrientation.LandscapeLeft))
{
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeLeft;
}

if ((iPhoneInput.orientation == iPhoneOrientation.LandscapeRight) (iPhoneSettings.screenOrientation != iPhoneScreenOrientation.LandscapeRight))
{
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeRight;
}

}

}


but I am getting a freeze screen when I start the app on my iphone. I think my code is badly ported to C sharp in this declaration part :

/JAVA - >/ var moveJoystick : Joypad;
/C sharp - >/ public Joystick moveJoystick; // it doesn’t let me put my joystick prefab on the editor like I do in java
// But in the java code I have no Transform or GameObject in this part, so what to do?

and in here I made this quick fix that I looked at google and didn’t find anything in start function:

cam = gameObject.FindWithTag(“MainCamera”).GetComponent(“Camera”); // JAVA
Camera cam = (Camera)gameObject.GetComponent(“Camera”); //C sharp quick fix???

So I let the code here and if you guys find it worth for an iphone Game controller please give me a hand on the C sharp that I need to take variables from another cs file , because i am pretty sure that it’s almost virtually impossible to take variables from C to java to use them globally…er ia mean publicly!

Any hints here? :expressionless:

FIXED I forgot to put my Joystick.cs and change my joypad.js on my joystickprefab and drag it and drop it on the editor!!

The code was fixed but my penelope joystick is not working anymore?
Why…on the unity + iOS pro it was working? And I haven’t touched anymore the code until I’bought the iOS basic. Is something wrong with my code?

Fixed the code and discovered that if you put create a GameObject variable and a script variable and in the editor if you drag and drop the same gameobject(the joystick) to both, the penelope joystick will stop working.The point is I was trying to instantiate the joystick and also use the script with the same gameobject and you can´t do this, so I fixed it creating another gameobject to spawn the joystick ( and other control types ) and with the first gameobject I used only the script variable + drag and drop with the editor( the joystick gameobject with the penelope script modified ). FIXED!!

I’m glad that’s over.

In the future, you can use tags to make your pasted code legible, and ideally only paste the parts that are giving you errors. In this case, the two lines you quoted at the very end of your second post were enough to see major errors.