Player Controls For Restricted Directional Movement, but those things vary...
So I am making a little game.
My camera is working pretty well now thanks to @Bunny83.
And having applied that same camera rotation code to my Playerobject, the player now continues moving in the same on-screen-direction even though the game-space has rotated by 90° in whichever direction.
Now, however, I am back to the problem of player control input.
I want the player to be able to use WSAD or similar 4-directional controls to move the player-object in the corresponding direction. Problem is that in worldspace, the true directions are reorienting all the time because the player-cam rotates around the play space.
I have tried using self-space rotation on the player object, tried various Vector.3 things.
I really don't want to have to end up using just two keys for Turn-90°-Left/Right, because that become difficult to interpret when the playerObject is not moving in a scree-vertical vector from bottom to top of the screen.
Any ideas, Oh Great Internets?
playerObject Code:
public float currentSpeed;
public float curRotX = 0f;
public float curRotY = 90f;
public float curRotZ = 0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
curRotX = gameObject.transform.rotation.x;
curRotY = gameObject.transform.rotation.y;
curRotZ = gameObject.transform.rotation.z;
ForwardMotion ();
RotatePlayer ();
RotatePlane();
}
// to cause constant motion.
void ForwardMotion ()
{
transform.Translate (Vector3.forward * currentSpeed * Time.deltaTime, Space.Self);
}
// to rate the player gameObject
void RotatePlayer ()
{
if (!(Input.GetKey (KeyCode.RightShift)))
{
if (Input.GetKeyDown(KeyCode.W))
{
transform.Rotate (curRotX, curRotY, curRotZ, Space.Self);
Debug.Log ("Player Turned North");
}
else if (Input.GetKeyDown(KeyCode.S))
{
transform.Rotate (curRotX, curRotY + 180, curRotZ, Space.Self);
Debug.Log ("Player Turned South");
}
else if (Input.GetKeyDown(KeyCode.D))
{
transform.Rotate (curRotX, curRotY + 90, curRotZ, Space.Self);
Debug.Log ("Player Turned East");
}
else if (Input.GetKeyDown(KeyCode.A))
{
transform.Rotate (curRotX, curRotY -90, curRotZ, Space.Self);
Debug.Log ("Player Turned West");
}
}
}
// to rotate the plane of play
void RotatePlane()
{
if (Input.GetKey(KeyCode.RightShift))
{
if (Input.GetKeyDown(KeyCode.W))
{
gameObject.transform.Rotate(0f, 0f, 90f);
}
else if (Input.GetKeyDown(KeyCode.S))
{
gameObject.transform.Rotate(0f, 0f, -90f);
}
else if (Input.GetKeyDown(KeyCode.D))
{
gameObject.transform.Rotate(90f, 0f, 0f);
}
else if (Input.GetKeyDown(KeyCode.A))
{
gameObject.transform.Rotate (-90f, 0f, 0f);
}
}
}
}
I have a screen capture video but UnityAnswers hates me and wont let me upload a 1.17MB MP4.
On the start plane it works just fine, I can WSAD my way to anywhere I want to go.
However, once I start rotating around the play-space, and thereby changing my plane-of-play, the controls become less useful, until I have to try to guesstimate how to use them and resort to just using my A & S keys, which are still often inverted due to my orientation.