- Home /
Android tilt implemented in FPC - code conversion
I have implemented the First Person Controller to my scene, and with my computer keyboard it works like a charm, all I want to do is, simply, implement one or two of the scripts that the FPC comes with for it to take the tilting movement of my android device rather than W/S/A/D. So tilt; Forward (W), Backwards(S) Right(D), Left(A)
I have done this with a C# script but I want it with a Js, most preferably to make changes in the scripts that the First Person Controller comes with.
I'm confused about your question. If you've done 'this' with a C# script, why not take a shot at converting it to JS and then ask for help where you have issues?:
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
Because I want to use the FPC thus, I want to implement it in its script but it is done with JS. Here is my C# script if anybody wants to give it a shot, which works perfectly: void Update () { float h = Input.GetAxis("Horizontal") Time.deltaTime moveSpeed; float v = Input.GetAxis("Vertical") Time.deltaTime moveSpeed;
float transH = 0;
float transV = 0;
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.x;
dir.y = Input.acceleration.y;
transH = dir.x * (moveSpeed + 10.0f) * Time.deltaTime;
transV = dir.y * (moveSpeed + 10.0f) * Time.deltaTime;
transform.Translate(transH, 0,transV);
float z = transform.position.z;
transform.Translate(h, v,0);
I'm suggesting you try and make the conversion yourself. Translating back and forth from C#/Javascript is a useful skill to have, and many scripts can be translated knowing just a handful of things. For the code you've posted, you only have to know two things, how to translate function declarations and how to translate variable declarations. If you get stuck, post both versions of your script and someone can then help you with any remaining problems.
Thank you for the encouragement Robertbu. I GOT IT!! Only with a $$anonymous$$or problem of course; here is the entire FPC control, commented out the old part with the new control added for android, IT WONT GO SIDE WAYS NOW:
private var motor : Character$$anonymous$$otor;
var transH = 0;
var transV = 0;
var dir = Vector3.zero;
// Use this for initialization
function Awake () {
motor = GetComponent(Character$$anonymous$$otor);
}
// Update is called once per frame
function Update () {
dir.x = Input.acceleration.x;
dir.y = Input.acceleration.y;
var h = Input.GetAxis("Horizontal") * Time.deltaTime * 3.0;
var v = Input.GetAxis("Vertical") * Time.deltaTime * 3.0;
transH = dir.x * (3 + 10.0f) * Time.deltaTime;
transV = dir.y * (3 + 10.0f) * Time.deltaTime;
transform.Translate(transH, transV,0);
var z = transform.position.z;
transform.Translate(v, h,0);
/* Get the input vector from kayboard or analog stick
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
//========================================
directionVector = directionVector * directionLength;
}*/
// Apply the direction to the Character$$anonymous$$otor
motor.input$$anonymous$$oveDirection = transform.rotation * dir;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (Character$$anonymous$$otor)
@script AddComponent$$anonymous$$enu ("Character/FPS Input Controller")
On a quick read, I only see one potential issue. In the original script, transH and transV are floating point values. In your new script they are integers. This means that on lines 17 and 18, your code will round the values assigned to ints. When you do:
var transH = 0;
The compiler will assume the type of the variable by what you assign it. In the original C# script, you assign an int also, but you've typed the variable as a 'float', so the compiler knows how to make the conversion.
So to get your float in Javascript you can do:
var transH = 0.0;
or
var transH : float = 0;
Your answer

Follow this Question
Related Questions
Object moving on its own 0 Answers
JS to C#, anyone? 1 Answer
whay cant i move with this script???? 3 Answers
how to only destroy certain blocks? 1 Answer
Will downgrading to a previous version of unity affect my project? 1 Answer