- Home /
Is there a character controller for mobile?
There is a built-in Character controller in Unity 3d 4.3. I'm wondering if there is a character controller for mobile platform that I can use touch or accelerometer to controller the movement of the character?
Or if there is a tutorial about how to modify the built-in character controller to support mobile platform?
Thanks
Answer by Morden · Sep 08, 2014 at 04:13 AM
The Why What Benproductions is trying to point out is that the CharacterController is one component that can be added to an object, and only handles the physics, not the movement. When you add one of the prefab character controllers (First Person Controller or Third Person Controller) to your game, you get an object with CharacterController attached, plus a CharacterMotor script and either the FPSInputController or ThirdPersonInputController (along with ThirdPersonCamera for the Third Person controller). These input controllers are what determine how the character actually moves. They direct the CharacterController to move, it merely handles collisions and such. So to move to a platform type game, you remove the current input controller script and add the PlatformInputController script. So if you wanted to handle different controls for android, you would either modify one of these input controller scripts, or roll your own with whatever controls you want for the mobile.
The How For the FPSInputController script and the PlatformInputController, one of the first lines in Update is something like:
var directionVector = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
Just change that line to use whichever parts of Input.accelerometer you want to use. A simple version that I did is just changing it to
var v = new Vector2(Input.acceleration.x, Input.acceleration.y).normalized;
var directionVector = new Vector3(v.x, v.y, 0);
That requires you to play with your tablet flat on a table, and doesn't allow any real zero point, so you always move. You would probably want to implement something in your interface to offset the acceleration vector to allow your player to hold the tablet however they wanted. You would probably also want to change the next lines, that check to see if the directionVector magnitude is > 0, to be something other than 0 to allow a "dead zone".
To do touch will be slightly more complicated, and will depend on how you want to do it. The simplest way I can think of is to set a static Vector2 called AimPoint in your InputController class that is accessible from other scripts. Then whenever you detect a touch on your game field, set AimPoint to the world coordinate. Then the inputController would calculate directionVector as an offset from the current position to the AimPoint. This should have your character moving toward wherever you touch.
For the third person controller, it's more complicated. I haven't dug into the code completely, but it looks like it is slowly applying your direction changes and such to get smooth cornering. But it still boils down to changing the input axes (plus however you want to handle jumping). Update calls UpdateSmoothedMovementDirection, which then sets v to Input.GetAxisRaw("Vertical") and h to Input.GetAxisRaw("Horizontal"). Just change those to set to either an offset/normalized vector to your touch location, or to the appropriate components of the acceleration vector.
Answer by Benproductions1 · Dec 25, 2013 at 07:40 AM
Hello,
The "Built-in" Character Controller does not come with any standard controls. It is merely a platform for character like movement which you have full implementation control over. It's a component comparable to the Rigidbody in many ways and just like it, has no mobile/desktop version. You also cannot modify the "built-in" Character Controller unless you decompile the Unity Editor and/or Physx which I think is against the terms and conditions.
Hope this helps,
Benproductions1
Then if it's possible to write an "adapter" to convert mobile touch input to keyboard (axis) movement? I think this should be very common usage. There must be somebody did this already. Anybody want to share ?
AS I said The character controller doesn't take any input. What So Ever. Not from the keyboard, not from anything! The CharacterController
component is merely another interface to the physics engine allowing for character like movement. How you perform this movement is up to you.
If I'm not wrong, I saw a tutorial about changing the Character Controller to support 2d only. That means it's possible to change the character controller script. Am I right?
You don't change the CharacterController in order to make it move, you use it in order to make its functions to make it move.
It's not a tutorial on changing the CharacterController
for 2D. It's a tutorial on how to use the functions of the CharacterController
component to act as a 2D object. Stop trying to argue, it's a matter of fact! Without decompiling the game engine, You cannot change or edit the CharacterController
.