Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

CharacterMotor, FPSInputController, PlatformInputController in C#

Discussion in 'Scripting' started by xadhoom, Oct 20, 2010.

  1. xadhoom

    xadhoom

    Joined:
    Sep 2, 2010
    Posts:
    91
    Hi,

    Since we are programming mainly with C# I had to convert some of the js character scripts shipped with Unity to C#. Before anybody else wastes his time for this here they are.

    If you encounter any problems/inconsistency using these compared with the js version please let me know.

    xad
     

    Attached Files:

  2. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,608
    Thanks for these.

    I'm still using my C# version of Eric5h5's upgraded first person controller, but that was mainly because CharacterMotor etc. was JS. :)

    I'll check these out when I get home.
     
  3. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    I am bumping this because this is great. Saved me a lot of trouble!
     
  4. nawash

    nawash

    Joined:
    Jan 29, 2010
    Posts:
    143
    Thank you, thank you and thank you again
    I was starting struggling with the Javascript version and interacting with c#...
     
  5. shaneneville

    shaneneville

    Joined:
    Jan 8, 2011
    Posts:
    7
    I've been having problems with the C# CharacterMotor script that only appear on iOS (it works fine in the IDE, webplayer and Mac builds)

    The following line causes my character to disappear when the game is running on iOS:

    Code (csharp):
    1. var projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity
    I changed the variable definition, but it still creates the same problem.

    Code (csharp):
    1. Vector3 projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);
    There are also a couple of other places in the CharacterMotor where var is used instead of float.

    If anyone has a fix for the Vector3.Project issue, any help would be greatly appreciated!
     
    Last edited: May 13, 2011
  6. spk

    spk

    Joined:
    Jan 9, 2009
    Posts:
    527
    Thanks for going through the pain of doing this and sharing it ;)
     
  7. MaxAttack

    MaxAttack

    Joined:
    Apr 25, 2012
    Posts:
    1
    Have you found the solution of your problem? I am in the same situation... what version of unity are you unsing and on what IOS?

    i'm on unity 3.4 with IOS 5
     
  8. shaneneville

    shaneneville

    Joined:
    Jan 8, 2011
    Posts:
    7
    Honestly? I just commented out the line and carried on. Vector3.Project seems to have some problems on iOS and since my game didn't use 'TooSteep()', I just took it out.
     
  9. mkuchenb

    mkuchenb

    Joined:
    Aug 16, 2012
    Posts:
    1
    I spent about 2 hours converting the CharacterMotor only to have it not work as expected. Thank you. Now I don't need to reinvent and find the structural problems with the wheel.
     
  10. xadhoom

    xadhoom

    Joined:
    Sep 2, 2010
    Posts:
    91
    Long time ago... you are welcome ;-)
     
  11. micsun

    micsun

    Joined:
    Mar 4, 2012
    Posts:
    8
    Thank you! Saved me some time :)
     
  12. _zeta

    _zeta

    Joined:
    Dec 24, 2011
    Posts:
    99
    a hey guys I got problem with this code...
    here is what I am getting:
    [​IMG]
    that happens every time I connect to my photon server with client and join a random team in result a player is spawned with this script...
    anywho could help me?
     
  13. nawash

    nawash

    Joined:
    Jan 29, 2010
    Posts:
    143
    Hard to answer ...
    The controller is NULL or "this" is NULL ....
    Hard to answer without more context.
     
  14. _zeta

    _zeta

    Joined:
    Dec 24, 2011
    Posts:
    99
    [​IMG]
    That is all I can give, I basicly dont get what is wrong with it, there is both controller and the gameobject exists...
    what now :(
     
  15. xadhoom

    xadhoom

    Joined:
    Sep 2, 2010
    Posts:
    91
    Hi,

    This looks a bit odd. Did you debug the code at this line (before the exception is thrown). What part of the equation leads to the issue? It looks very much as if the controller is null...

    Maybe the order of script initialization is a problem here.

    xad
     
  16. _zeta

    _zeta

    Joined:
    Dec 24, 2011
    Posts:
    99
    defaq, I added debug.log and it says its null..............now what :(
    its paradox, I looked on player prefab both playing and in editor and both cases I saw the Character Controller,
    yet the dumb script dosent see it :mad: oh and about the script init...
    script is lockated in "Assets/Scripts/".
    and also the Execution Order is default.
    please guys help :( I am totaly lost :(
     
  17. xadhoom

    xadhoom

    Joined:
    Sep 2, 2010
    Posts:
    91
    Did you check that the controller has been set before the call? Btw. I recommend to use Monodevelop debugging for easy stepping through the code here. log messages might be a bit limited here.

    I see that the FixedUpdate() method leeds to the call of the method. At least at this point the Awake() and Start() methods should have been called...
     
  18. _zeta

    _zeta

    Joined:
    Dec 24, 2011
    Posts:
    99
    well it does
    controller = this.controller inside of awake function...
    that is the only place where it is decleared...
    maybe some other method is required? if so please do write down witch :)
     
  19. _zeta

    _zeta

    Joined:
    Dec 24, 2011
    Posts:
    99
    oh damn, I found the problem, :D
    now its solved :D
    sadly Collage starts today :(
     
  20. Poemind

    Poemind

    Joined:
    May 25, 2011
    Posts:
    26
    I'm hoping that these will work without much modification but serious thanks for this! I was trying to do the same sort of conversion and i thought i'd search first. Glad I did!

    Thanks !

    EDIT:
    Ok, This is great but when I try to add these scripts to my PlayerController I get and error saying "Can not add scrip. Script needs to finish compiling before adding it" or some such.

    Any ideas why this would happen?
     
    Last edited: Oct 22, 2012
  21. xadhoom

    xadhoom

    Joined:
    Sep 2, 2010
    Posts:
    91
    As stated this happens if the whole project does not compile due to compile errors in a file of your project. Unity does not allow attaching scripts before everything is compiled since it needs to run some reflection code on the component to grab the serializable fields, event methods etc.
     
  22. penna91

    penna91

    Joined:
    Nov 2, 2011
    Posts:
    21
    Works fine, i love you =)
     
  23. xvart

    xvart

    Joined:
    Dec 12, 2010
    Posts:
    19
    Made my midnight!
     
  24. Bmxchamp4

    Bmxchamp4

    Joined:
    Feb 13, 2013
    Posts:
    9
    Oh my god this was a life saver. Saved me so much time! Thanks for the post :)
     
  25. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    You rock, I was about to do this myself, thanks a ton!
     
  26. JDMulti

    JDMulti

    Joined:
    Jan 4, 2012
    Posts:
    352
    I tried to use this c# Character Motor script, and it seams to work fine in the Editor.
    However, as soon as I publish a Web player version, the gravity and controlling the character is totally different. It seams I'm standing on the Moon, everything is with a low gravity somehow.
    However when I go back into the Editor, everything seams to be fine.

    Anyone else has had this problem with the C# Character Controller, do I need a new version. If so, where can I find that new version?
     
  27. xadhoom

    xadhoom

    Joined:
    Sep 2, 2010
    Posts:
    91
    Hi JD,

    Very hard to assess. I did not use the scripts in the latest versions of Unity, but are not aware of any changes which could lead to the artefacts you describe. Did you check the log for any errors at runtime? Do you see a way to runtime debug the issue in more detail?

    xad
     
  28. JDMulti

    JDMulti

    Joined:
    Jan 4, 2012
    Posts:
    352
    There are no errors nor warnings, it just works fine in the Editor without any hassle. As soon I build a web version it seams I'm walking on the moon.
    Here is a link to the webplayer. It's a wip, but just to check it yourself: http://bit.ly/131T9bb ( 30 mb download, can take a while )
     
  29. plingativator

    plingativator

    Joined:
    May 2, 2013
    Posts:
    12
    Thank you! This should really be included by default with the package in Unity. Appreciate your sharing! Saved me more time than I can imagine.
     
  30. JDMulti

    JDMulti

    Joined:
    Jan 4, 2012
    Posts:
    352
    Sorry for the reply on my own post, but this is for future reference for anyone having the same problem.

    I fixed this bug. It seamed that when you have the javascript version of Character Motor script and the C# in the same project. There seams to be an conflict happening on the serialization of publishing your project. No errors or warnings are output to the console, but what happens behind the scene is this.

    A script written in javascript and C# with the same name cause a conflict and corrupts the game data. Which means strange things occur when you publish your game. From freezing standalone game, to strange behaviors in the webplayer game experience.

    You fix it simply by:
    - Make sure no script is named the same, no matter if one of them is written in a different language. It will cause a conflict. Just remove one of them will fix the serialization conflict.

    I hope it helps someone, took me about 2 weeks to find this issue, because the errors written down in the standalone development build aren't referencing any logical explanation. Because it said, your data is corrupted, try remove it. Another error was: object position out of bounds. I had to Google for solutions and found an old topic about this error.
     
  31. Setmaster

    Setmaster

    Joined:
    Sep 2, 2013
    Posts:
    190
    Thanks.
     
  32. mcslibin

    mcslibin

    Joined:
    Sep 13, 2013
    Posts:
    1
    I don't normally respond to any kind of threads, but you seriously saved me a lot of time, and for that I wanted to be sure to say thanks and give some constructive feedback since I'll be using this as well.

    So thank you very much!

    I did a quick comparison of all the conversions in BeyondCompare, and it looked like a solid conversion (with a couple value differences). I do have a couple of things to note, however (only pertains to CharacterMotor.cs):

    1.

    While it was a literal translation of values, Unity (as far as I've read) has an odd habit of caching the Inspector values without ever updating the script behind the scenes. That said, I believe that when you first create a CharacterController, it inherently uses different values than what is in the CharacterMotor script (e.g. movement speeds, jump values, slide speed).

    You may notice when you first use the C# version, it feels different running and jumping than if you are using a clean CharacterController with the native javascript code behind. The values are the same in both files, but something is still off. I believe this is the caching issue I read about, as you can see that the values in the Inspector are different than what is in the javascript for a clean controller.

    This is easily remedied, however, by just copying down the native values from the inspector to the C# CharacterMotor file.

    2.

    You must explicitly label the canControl and useFixedUpdate variables at the top of the C# CharacterMotor file as "public" if you want to be able to see them in the Inspector, like you can with the javascript file.


    Hope that helps someone else out there. And seriously, thanks again, xadhoom. I really appreciate not having to reinvent the wheel.
     
    Last edited: Oct 5, 2013
  33. Airrender

    Airrender

    Joined:
    Dec 6, 2012
    Posts:
    5
    I must say, this saved my day. I was about 40 lines into converting that looooooooong CharacterMotor script to C# myself when i decided to have a look and see if anyone else had already done this. Found one or two, but all of them were modified in some form or another. Couldnt find an EXACT conversion of functionality so i just went back to doing it myself. Took a break, came back, decided to look a little more and thankfully found this thread. Probably wouldve taken me a good bit into the morning to convert the code myself, so i feel obligated to say thank you for this wonderful post.
     
    Last edited: Nov 10, 2013
  34. FabioZ84

    FabioZ84

    Joined:
    Dec 9, 2012
    Posts:
    2
    Thank you so much!!
     
  35. warmonked

    warmonked

    Joined:
    May 28, 2013
    Posts:
    1
    Xad,
    You are a great human being.
     
  36. GameMakerMiller

    GameMakerMiller

    Joined:
    Jan 26, 2013
    Posts:
    1
    You will be my first time commenting on these forums.

    Four years later and this is still useful. I was just about to upgrade all of the java files to C#for ease of use on my end, I am very glad that I assumed I wasn't the first to think to do so.
     
  37. kersk

    kersk

    Joined:
    Jan 2, 2012
    Posts:
    53
    I got about 30 lines into doing this while updating a project and decided to do a search instead. Just wanted to say thanks for saving me some time.
     
  38. Nirvan

    Nirvan

    Joined:
    Nov 16, 2013
    Posts:
    59
    Guys :( I made exe and tested it on two computers and with this code when I make any step ( left/back/forward) everything turning black and application not responding ( but in editor works well ) and js version works well.
     
    Last edited: Feb 21, 2014
  39. Mr.Drew

    Mr.Drew

    Joined:
    Jul 18, 2013
    Posts:
    1
    Hey,

    I'm having the same problem, but with Android.

    When I move the player in android, its position changes to NaN.
    It works in the editor and through the Unity Remote tool.

    Anyone had any luck fixing that?

    Thanks,
    Drew
     
  40. friedfish

    friedfish

    Joined:
    Jan 22, 2013
    Posts:
    35
    This really helps! Thanks alot! Truly enough before anyone wastes time converting.

    Definitely crediting you for the conversion. :)

    I'll post for any problems encountered on the script conversion.
     
  41. Crocz

    Crocz

    Joined:
    Apr 26, 2014
    Posts:
    1
    Thanks, saved me a lot of time! :)
     
  42. Sinedia

    Sinedia

    Joined:
    Oct 23, 2014
    Posts:
    1
    Thanks... was just about to do this myself when I found your post... saves me some time :)
     
  43. HiAvatar

    HiAvatar

    Joined:
    Jan 9, 2015
    Posts:
    2
    You are a nice man! thank you.
     
  44. Prophecy420

    Prophecy420

    Joined:
    Jan 21, 2015
    Posts:
    1
    Hi,
    I'm having a half-problem with the scripts.
    When I use them, they work (in that I can move and jump). But I get 2 warnings that say "The referenced script on this Behavior is missing." Also, the movement appears to be slower, although that could just be the script and I could make modifications.

    I'm new to using Unity, so I'm not sure if I did something incorrectly. I took the pre-made character controller and just swapped out the Javascripts for the C# scripts. I'm assuming I need to remove or change something that is still trying to call the Javascript? Or do I need to create a whole new character controller with the new scripts? I'm not sure how to do that if I do need to.

    Thanks.
     
  45. opponent019

    opponent019

    Joined:
    Feb 6, 2014
    Posts:
    18
    Since this is 5 years old I was thinking it won't work, but I gave it a try anyways... it still works! :D
    Thank you! n.n
     
    Last edited: Jan 28, 2015
  46. ladismad

    ladismad

    Joined:
    Nov 24, 2014
    Posts:
    1
    Really needed this- thanks :D
     
  47. Erenulu

    Erenulu

    Joined:
    Apr 25, 2016
    Posts:
    2
    Error:
    Vector3 projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);

    Fix? pls :(
     
  48. Erenulu

    Erenulu

    Joined:
    Apr 25, 2016
    Posts:
    2
    Vector3 projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);

    Error
    Fix Error? pls :(
     
  49. Lorddazzor

    Lorddazzor

    Joined:
    Oct 24, 2016
    Posts:
    1
    Thanks mate this is the best thing ever
     
  50. WiferCo

    WiferCo

    Joined:
    Sep 9, 2017
    Posts:
    1
    GRASIAS MUCHAS GRASIAS :'V