0
\$\begingroup\$

Right now I have a small soldier on the screen that moves with W, A, S, D and rotates / changes direction with the arrow keys. What I want to happen is when the player's direction changes (by using the arrow keys), pushing W will send him in that direction instead of up the screen.

Also (for example), if you are holding D and the right arrow key at the same time he should move in a circle about a point on the ground (instead of spinning in a circle while moving in a straight line to the right).

Here is my current code:

public class MovePlayer : MonoBehaviour {
  void Update() {
    // Rotate left
    if (Input.GetKey (KeyCode.LeftArrow)) {
      transform.Rotate(0, 0, 1.3f);
    }
    // Rotate right
    if (Input.GetKey(KeyCode.RightArrow)) {
      transform.Rotate(0, 0, -1.3f);
    }
    // Strafe left
    if (Input.GetKey (KeyCode.A)) { 
  Vector3 position = this.transform.position;
      position.x -= 0.055f;
      this.transform.position = position;
    }
    // Move up      
    if (Input.GetKey (KeyCode.W)) {
      Vector3 position = this.transform.position;
        position.y += 0.043f;
        this.transform.position = position;
    } 
    // Move down        
    if (Input.GetKey (KeyCode.S)) {
      Vector3 position = this.transform.position;
      position.y -= 0.043f;
      this.transform.position = position;
      isMoving = true;
    }
    // Strafe right     
    if (Input.GetKey (KeyCode.D)) {
      Vector3 position = this.transform.position;
      position.x += 0.055f;
      this.transform.position = position;
    } 
}

}

Any ideas? Wasn't sure what to search exactly.

\$\endgroup\$
1
\$\begingroup\$

I don't fully understand your question.

But, if you want to move something forward, what I would do is something like:

this.transform.position += this.transform.forward * moveSpeed;

Whereas, moveSpeed is the speed at which you want your unit to go forward.

To go in the opposite direction that you are facing:

this.transform.position -= this.transform.forward * moveSpeed;

Try giving this a try and seeing it it helps:

private float moveSpeed = 1;

void Update() {
    // Rotate left
    if (Input.GetKey(KeyCode.LeftArrow)) {
        transform.Rotate(0, -1, 0);
    }
    // Rotate right
    if (Input.GetKey(KeyCode.RightArrow)) {
        transform.Rotate(0, 1, 0);
    }
    // Strafe left
    if (Input.GetKey (KeyCode.A)) { 
        transform.position -= new Vector3(1,0,0);
    }
    // Move forward
    if (Input.GetKey (KeyCode.W)) {
        transform.position += transform.forward*moveSpeed;
    } 
    // Move backward
    if (Input.GetKey (KeyCode.S)) {
        transform.position -= transform.forward*moveSpeed;
    }
    // Strafe right     
    if (Input.GetKey (KeyCode.D)) {
        transform.position += new Vector3(1,0,0);
    } 
}
\$\endgroup\$
  • \$\begingroup\$ The above is for going 'forward' along the Z 'blue' axis, but you can do the same for 'right' (aka 'red' axis) or 'up' (aka 'green axis'). For what you described, you most likely would use forward. \$\endgroup\$ – return true May 16 '15 at 22:16
  • \$\begingroup\$ This code doesn't work for me for some reason. I mean (for instance) I want my 'W' key to make my player travel in the direction he is facing rather than straight up the screen. \$\endgroup\$ – eyes enberg May 16 '15 at 22:26
  • \$\begingroup\$ Can I see what code you are using? Maybe I can help \$\endgroup\$ – return true May 16 '15 at 22:28
  • \$\begingroup\$ Literally changed the if-statement for my 'W' key to contain just the line this.transform.position += this.transform.forward * 10f; \$\endgroup\$ – eyes enberg May 16 '15 at 22:50
  • \$\begingroup\$ I updated my answer - let me know if that code at the bottom works for you \$\endgroup\$ – return true May 16 '15 at 22:51
0
\$\begingroup\$

Use transform.TransformDirection() along with Vector3D.forward (or .up or whatever). TransformDirection() will convert the 'forward' direction of the player to that direction in global space.

EDIT: oops you want to transform the direction the other way, from world space to local space, not local to world. Use InverseTransformDirection()


Another simple approach would be to separate the rotating visual from moving object, so that the object you moves never rotates. In that case, just parent the character to the moving object.

\$\endgroup\$
  • \$\begingroup\$ How would I use this? I have tried adding transform.InverseTransformDirection(Vector3.forward) to the 'W' key event but it doesn't seem to do the trick. \$\endgroup\$ – eyes enberg May 16 '15 at 22:31
  • \$\begingroup\$ That's pretty much the idea, but from the looks of your code you want Vector3.up; you are moving in the Y direction, which is up/down. \$\endgroup\$ – jhocking May 17 '15 at 1:25
  • \$\begingroup\$ For some reason it still doesn't work. Can you show me what my method for W should look like? (I forgot to mention that my game is in 2D btw) \$\endgroup\$ – eyes enberg May 17 '15 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.