- Home /
How to convert joystick position to rotation?
I have been working through the Penelope script to get my head round linking a GUI joystick to a Game Object. I have a scene setup with a simple cube object driven by the joystick.
The following code works fine and will move the cube around when I move the joystick pad:
var moveJoystick : joystickControl; //the enumerated joystick pad
var objectTransform : Transform; //a simple cube
function Update()
{
var cubeMovement = moveJoystick.position;
objectTransform.Translate (cubeMovement.x, 0, cubeMovement.y, 0 );
}
However, as soon as I try something similar using rotation - it has no effect. The cube does not spin/move at all:
var rotateJoystick : joystickControl; //the enumerated joystick pad
var objectRotate : Transform; //the simple cube object
var rotationSpeed : Vector2 = Vector2( 50, 25 );
function Update()
{
var cubeRotate = rotateJoystick.position; // Scale joystick input with rotation speed
cubeRotate.x *= rotationSpeed.x;
cubeRotate.y *= rotationSpeed.y;
cubeRotate *= Time.deltaTime;
objectRotate.Rotate (cubeRotate.x, cubeRotate.y, 0, 0 );
}
I tried to reverse engineer the tutorial so I could pull out a better understanding of the principles, however I am clearly missing something fundamental. Could anyone point me in the right direction? I have searched and tried variations on the above rotation code but to no avail. Is it anything to do with euler/quaternion?
I assume that rotateJoystick.position returns a Vector2 between -1 and 1. You could try modifying my script here : http://answers.unity3d.com/questions/289055/rotate-on-drag-for-ios.html
Remove all the touch stuff and use rotateJoystick.position.x ins$$anonymous$$d of touch.deltaPosition.x
Your answer

Follow this Question
Related Questions
advanced problem for me regarding transfering rotation from the gizmo to the object 0 Answers
stay the rotation in character 1 Answer
why is my character rotation always 180° after joystick is released. 1 Answer
Rigidbody rotation with virtual joystick, weird behaviour 0 Answers
Setting up a rotation joystick 0 Answers