Search Unity

[FPS] Getting current direction state from player movement

Discussion in 'Scripting' started by Adweril, Jul 28, 2014.

  1. Adweril

    Adweril

    Joined:
    Apr 8, 2014
    Posts:
    2
    Hello,

    I have a classic FPS setup where my character can:
    - Press A/D to strafe left and right
    - Press W/S to move forward/backward
    - Rotate using the mouse

    For reasons specific to my context, I would like to detect when the player is doing the following:
    - Full strafe; left or right
    - Forward + Strafe; left or right
    - Backward + Strafe; left or right
    - Full forward
    - Full backward

    I would like to solve this only from the player movement. (No Input.GetAxis() )

    I have the character forward vector, which direction is changed when we rotate with the mouse.
    I have the position, and the previous position, so I can compute a "velocity" vector to know where the character is going/coming.

    Is there a clean way of doing this with some vectors math (Dot , Cross product) ?
    I've been struggling for hours trying to figure out the right way of doing this...

    Thanks in advance
     
    Last edited: Jul 28, 2014
  2. Adweril

    Adweril

    Joined:
    Apr 8, 2014
    Posts:
    2
    No replies :(

    Is my approach all wrong or unusual ?

    The reason I want to compute this from the movement is because it's a multiplayer game, and I have to figure out which animation to play based on the direction state (I have an animation foreach state)

    I don't have the animations synced over the network. Instead, I'm calculating which one to play based on the velocity of the player, etc.

    But for the "diagonal speed", I can't figure out... :(
     
  3. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Based on the explanation you gave, what you're trying to do is as simple as detecting the velocity on your character's rigidbody. The simplest method for detecting it is explained (in pseudo) below:

    let vel = rigidBody.velocity:

    if(vel.y > 0 && vel.x == 0) then FORWARD_ONLY
    if(vel.y < 0 && vel.x == 0) then BACKWARD_ONLY
    if(vel.y == 0 && vel.x < 0) then LEFT_ONLY
    if(vel.y == 0 && vel.x > 0) then RIGHT_ONLY
    if(vel.y > 0 && vel.x > 0) then FORWARD_RIGHT
    if(vel.y > 0 && vel.x < 0) then FORWARD_LEFT
    if(vel.y < 0 && vel.x > 0) then BACKWARD_RIGHT
    if(vel.y < 0 && vel.x < 0) then BACKWARD_LEFT
     
    Last edited: Jul 28, 2014