- Home /
How do I controll a Character using the Mecanim Animator with Player Joysticks?
So i made a character and followed the tutorial at https://www.youtube.com/watch?v=Xx21y9eJq1U
That like is a tutorial on how to integrate the Mecanim Animator with your characters with your keyboard.
So my question is how do you integrate is this with the Player Relative Joysticks?
Answer by meat5000 · Aug 31, 2013 at 12:07 AM
The keyboard handles axes the same as anything else.
Look at AngryBots tutorial project at the FreeMovementMotor and PlayerMoveController scripts.
They contain things like:
#if UNITY_IPHONE || UNITY_ANDROID
if (joystickPrefab) {
// Create left joystick
var joystickLeftGO : GameObject = Instantiate (joystickPrefab) as GameObject;
joystickLeftGO.name = "Joystick Left";
joystickLeft = joystickLeftGO.GetComponent.<Joystick> ();
// Create right joystick
joystickRightGO = Instantiate (joystickPrefab) as GameObject;
joystickRightGO.name = "Joystick Right";
joystickRight = joystickRightGO.GetComponent.<Joystick> ();
}
Which helps you put many control methods in, platform dependant.
And look at this!
function Update () {
// HANDLE CHARACTER MOVEMENT DIRECTION
#if UNITY_IPHONE || UNITY_ANDROID
motor.movementDirection = joystickLeft.position.x * screenMovementRight + joystickLeft.position.y * screenMovementForward;
#else
motor.movementDirection = Input.GetAxis ("Horizontal") * screenMovementRight + Input.GetAxis ("Vertical") * screenMovementForward;
#endif
Input.GetAxis ("Horizontal") and Input.GetAxis ("Vertical")
Brilliant eh?
It does pay to check out the Input manager, but to be honest I don't use it.
Answer by Hyperion · Aug 30, 2013 at 12:47 AM
I won't be specific, but the way you do it without using the code in that tutorial is to set buttons as the Player Joystick buttons so that later you can say in your script:
if (Input.GetButtonDown("yourJoystick")){
//connect to the state machine and do desired action
}
would i need to change the input settings in the player prefrences to do this?
the joysticks are GUIs on mobile devices that you can move around on the screen by touching them
Yes. Unfortunately I am not so experienced with touch screen controls, but knowing $$anonymous$$ecanim, all you need to do to make it work is SO$$anonymous$$EHOW manipulate its parameters in the state machine. ($$anonymous$$g. you want to get to this: yourAnimator.SetBool("ExampleVariable",true) inside the code, and it'll work.) If you need any help with the $$anonymous$$ecanim side of coding, feel free to ask!
$$anonymous$$ecanim has no real bearing on the control method. Just GetAxis and AddForce in the direction you need. Statemachine should be set to handle the rest.
Yup, I agree with meat5000. As long as your GUI manipulates the state machine, $$anonymous$$ecanim will work. It's exactly like normal $$anonymous$$ecanim, except the interaction buttons are different.
Check how awesome I am at ripping code, below :P
That AngryBots tutorial has some awesome stuff worth learning in it!