Rotation and Movement on a single joystick!!!(iPhone)

Hi,

How can i have Rotation and Movment on a single joystick?

For example when i move up the joystick my character rotate to up and move up.i want to control character rotation and movement with a single joystick.my camera is fixed.

Thanks.

4 Answers

4

You could just make your controller point to the direction the joystick does in your moveDirection.
However, to use very smooth rotation when the character is not facing the correct direction yet, I’m used to do the following:

  1. I create an empty gameObject and parent it to the character making sure the empty’s transform is at (0,0,0).
  2. I move this empty object like it were the joystick itself in world space. Relative to the joystick’s axis.
  3. Than I use Quaternion.Slerp() to make the CharacterController smoothly rotate to the direction that empty object is pointing.

To move the empty object I use this:

   moveDirection= new Vector3(vector.x,0,vector.z);//vector is the joystick's axis
   //Controller movement stuff, etc... (omited)
   target.transform.position = new Vector3(transform.position.x+(vector.x*10),0,
       controller.transform.position.z+(vector.z*10));//target is the empty object

Than I rotate the character to that position where the empty object is, without the need of a second joystick:

if (Vector3.Distance(target.transform.position,transform.position) > 1) {
	Quaternion lookDirection = Quaternion.LookRotation(target.transform.position - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation,lookDirection,speed*Time.deltaTime);
}

You can do the same without an empty gameObject as a virtual joystick in 3D space, but math will be a bit more confusing.

So thanks.i'll give the result.

I tested the code.it works but my object direction is not coordinate with joystick direction!what should i do? by the way how can i have joystick rotation in circle space?

Just google on "Unity, joystick radius" to know how to get a circular input value. My personal joystick code is very different from those you'll see, I get input from direct pixel insets because the joystick I coded has an unknown size until the game start, but the radius thing is almost the same.

This can help too, for a better joystick design: http://www.gamasutra.com/view/feature/6323/a_guide_to_ios_twin_stick_shooter_.php?print=1

@brunoXavier how do you set vector x and vector z to the joystick axis? Is that the same as joystick.position.x? I've been trying to do what you said but I can't get it to work it's driving me crazy

Hi you can check this thread : http://forum.unity3d.com/threads/138973-Joystick-For-Mobile-(Third-Person-Controller)-Released-V1.1

Does anyone have a script for this please!

Anyone have a soulution that works with unity 3.4?

can you show us the full hierachy that this GameObject is parented to? Look like you got a gameobject that the main camera and ''Ai'' is parented, got something that say it to scale