Rotation used as movement

Hi everybody.
I’m trying to setup a control scheme using only object rotation. If the object rotates on Z, it moves forward/backward on X. If it rotates on X, it moves on Z.

Imagine someone trying to balance a broomstick on their hand. If it tilts forward, the person must move forward to keep it in balance. That’s the general idea I’m going for.
I’m a good artist but very new at scripting, so I’m probably going about this the worst way possible. Here’s what I have so far:

var rotSpeed = 5.0;
var traSpeed = 0.5;

function Update () {

	var rotX = Input.GetAxis("Mouse Y") * Time.deltaTime * rotSpeed;
	var rotZ = Input.GetAxis("Mouse X") * Time.deltaTime * rotSpeed;
	
	var traX = rotZ * traSpeed;
	var traZ = rotX * traSpeed;
	
	transform.Rotate(rotX,0,rotZ);
	transform.Translate(traX,0,traZ);

}

You’re just looking for general advice? You don’t have any actual issues with your code so far?

It looks like you’ve got the right idea to me, and I don’t see how it could be any simpler.

If it really was like a broomstick, it’d be a lot more complicated, though. To get the broomstick upright again, you have to actually outpace the stick for a short time to make it be upright again.

Thanks for replying!

“To get the broomstick upright again, you have to actually outpace the stick for a short time to make it be upright again.”

Absolutely no idea what that means, sorry.
As it is, the rotation input is directly tied to the translate, which makes the character move pretty freely and not in the direction of the rotation. I also need the character to move in a continuous direction as long as it is tilted in that direction.
I hope that clears things up a bit.

If you want to do something like a Segway, that moves to the side you tilt, you can’t use Rotate - Rotate will spin the object continuously in the direction you’re going. You should set the eulerAngles and calculate the velocity proportionally to the joystick input:

var traSpeed = 6.0;
var maxAngle = 20.0;

function Update () {
    // get the value of the axes (-1..+1)
    var rotX = Input.GetAxis("Vertical");
    var rotZ = Input.GetAxis("Horizontal");
    // set the eulerAngles
    transform.eulerAngles = Vector3(rotX * maxAngle, 0, -rotZ * maxAngle);
    // calculate the displacement (frame rate independent)
    var traX = rotZ * traSpeed * Time.deltaTime;
    var traZ = rotX * traSpeed * Time.deltaTime;
    // and move the object
    transform.Translate(traX, 0, traZ, Space.World);
}

Hey guys. I’m almost 90% of the way there and wanted to share. I got the mouse controls to work, up=forward, down=backward, left and right. However, if I do a circle, which combines movements, it tends to get stuck or stutter for a sec or so. Here’s what I got:

var mousePos : Vector2 = Input.mousePosition; //Mouse position
var maxX = Screen.width/2; //Changes mouse position on X
var maxZ = Screen.height/2; //Changes mouse position on Y

var mouseDistance : Vector2 = mousePos - new Vector2(maxX, maxZ);
var rotX = mouseDistance.x / maxX;
var rotZ = mouseDistance.y / maxZ;

transform.eulerAngles = Vector3(rotZ * maxAngle, 0, -rotX * maxAngle);

var traX = rotX * traSpeed * Time.deltaTime;
var traZ = rotZ * traSpeed * Time.deltaTime;

var controller : CharacterController = GetComponent(CharacterController);
var moveDir : Vector3 = Vector3(traX, 0, traZ);
controller.Move(moveDir);