- Home /
How to make a character move on Local axis?
The code below is simplified, but works perfectly when the character is facing the right direction. But when they turn, they move on the global scale, not the local. They are not a child of any other object, so localPosition is the same result.
I can't seem to figure out how to make the playerObject transform on the local axis. Is there a way?
moveX = currentTouchX - joystickCenterX;
moveY = currentTouchY - joystickCenterY;
playerObject.transform.position.z -= ((moveX * 100) * Time.deltaTime);
playerObject.transform.position.x += ((moveY * 100) * Time.deltaTime);
OR ----
Is there a way to get the Local x/y touch of the control wheel? The wheel looks like this, with "North" up and South down etc:
I was thinking a good alternative would be that as the character rotates, the wheel rotates as a compass. However, I could not figure out how to make the code below register where the on the wheel (localPosition) the touch was. Any one know how to do that? (This code uses mouse, for testing).
var screenPos2e = Input.mousePosition;
var worldPos2e = guiCamera.ScreenToWorldPoint(screenPos2e);
currentTouchX = worldPos2e.z;
currentTouchY = worldPos2e.y;
currentTouchZ = worldPos2e.z;
var centere = Vector2(joystickCenterX, joystickCenterY);
var currente = Vector2(currentTouchX, currentTouchY); //<--- This! How to make this the local x/y of the wheel?
Answer by infinitypbr · Feb 06, 2013 at 05:54 AM
My programmer brother was kind enough to help me out (even though he was working on programming stuff all day!).
The solution looks like this:
var playerAngle = Mathf.Deg2Rad * (360 - playerObject.transform.eulerAngles.y);
var x2 = (moveX * Mathf.Cos(playerAngle)) - (moveZ * Mathf.Sin(playerAngle));
var z2 = (moveX * Mathf.Sin(playerAngle)) + (moveZ * Mathf.Cos(playerAngle));
playerObject.transform.position.z += ((z2 * 100) * Time.deltaTime);
playerObject.transform.position.x += ((x2 * 100) * Time.deltaTime);