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 /
avatar image
0
Question by raklar · Oct 20, 2013 at 05:01 AM · movement scriptconversionunity-android

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.

Comment
Add comment · Show 8
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 robertbu · Oct 20, 2013 at 05:37 AM 0
Share

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

avatar image raklar · Oct 20, 2013 at 06:02 AM 0
Share

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);
avatar image robertbu · Oct 20, 2013 at 07:55 AM 0
Share

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.

avatar image raklar · Oct 20, 2013 at 08:07 AM 0
Share

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")
 
avatar image robertbu · Oct 20, 2013 at 03:52 PM 0
Share

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;
Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image avatar image

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


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