- Home /
Controlling the Character Controller Using UI?
I have a First Person Controller, and it is currently controlled by W, A, S, D, and Spacebar. I would like to make it so I can have on screen controls (For exporting to Android).
How can I:
Use the controls as buttons?
Code it into the CharacterMotor.js or whatever to use them as inputs instead of W, A, S, D and Spacebar?
Answer by Regalith · Feb 22, 2014 at 01:24 AM
Well you wouldn't have to alter Character Motor at all. What you do need to do is alter FPSInputController
function Update () {
// Get the input vector from keyboard or analog stick
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
simply put, you need to alter the Vector3 created here by inputing your own values that you get from detecting when a button is pushed or not. for example here's something you could do:
//this is FPSInputController
private var hMovement:float = 0;
private var vMovement:float = 0;
function Update()
{
//this is the only change in Update (the first line of update)
var directionVector = new Vector3(hMovement, 0,vMovement);
}
public function UpdateMovement(var x:float, var y:float)
{
xMovement = x;
yMovement = y;
}
Then all you need to do call the UpdateMovement function from your GUI script. You know you could also use Unity's Mobile Controllers if you wanted to since it'd result in alot less hacked together code.
I split the function into X and Z. I now have successfully overridden the walking controls! (: Thank you for your help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134309im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
pivot of CharacterController`s collider is not the pivot of the charcter???? 0 Answers
How to remove mouse icon with character controller 0 Answers
Sliding down slopes on the Character Controller? 0 Answers
Controlling real world object from a Unity model 0 Answers
Third Person Controller shuffling 0 Answers