Unity: Player slows down when looking in certain direction












0















I added a strafing mechanic to my Player with the help of the stackoverflow community and since then I have a weird bug:



When I rotate the Player (via Mouse input) towards the negative z direction my Player slows down (walking or running slows down massively).



Before I added the strafing mechanic the Player was controlled via W,A,S,D and the Mouse, the mouse rotates the Player, W is walking forward and A,S,D turned the Player to the left, back or right and them walked in this direction.



Since I added the strafing mechanic my Player could just walk, run and strafe while looking in the z direction. As soon as you turn around the whole Movement slows down.



void Update () {
// input
Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
Vector2 inputDir = input.normalized;
bool running = Input.GetKey (KeyCode.LeftShift);

if (!Input.GetMouseButtonDown(0) || Input.GetKey(KeyCode.JoystickButton2)) {
Move (inputDir, running);
Time.timeScale = 1;
}

if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.JoystickButton0)) {
Jump ();
}
// animator
float animationSpeedPercent = ((running) ? currentSpeed.magnitude / runSpeed : currentSpeed.magnitude / walkSpeed * .5f);


void Move(Vector2 inputDir, bool running) {


float targetRotation = cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));


Vector2 targetSpeed = new Vector2(
((running) ? runSpeed : walkSpeed) * inputDir.normalized.x,
((running) ? runSpeed : walkSpeed) * inputDir.normalized.y);

currentSpeed = Vector2.SmoothDamp(currentSpeed, targetSpeed,
ref speedSmoothVelocity,
GetModifiedSmoothTime(speedSmoothTime));

velocityY += Time.deltaTime * gravity;
Vector3 velocity = (transform.forward * currentSpeed.y) +
(transform.right * currentSpeed.x) +
Vector3.up * velocityY;

controller.Move(velocity * Time.deltaTime);
currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z);

if (controller.isGrounded)
{
velocityY = 0;
}
}


I expected the script to turn the player via Mouse input and move him via the keyboard. Of course i did not expect him to slow down if he faces an other direction. Looking forward to your help.



-nailuj05










share|improve this question





























    0















    I added a strafing mechanic to my Player with the help of the stackoverflow community and since then I have a weird bug:



    When I rotate the Player (via Mouse input) towards the negative z direction my Player slows down (walking or running slows down massively).



    Before I added the strafing mechanic the Player was controlled via W,A,S,D and the Mouse, the mouse rotates the Player, W is walking forward and A,S,D turned the Player to the left, back or right and them walked in this direction.



    Since I added the strafing mechanic my Player could just walk, run and strafe while looking in the z direction. As soon as you turn around the whole Movement slows down.



    void Update () {
    // input
    Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
    Vector2 inputDir = input.normalized;
    bool running = Input.GetKey (KeyCode.LeftShift);

    if (!Input.GetMouseButtonDown(0) || Input.GetKey(KeyCode.JoystickButton2)) {
    Move (inputDir, running);
    Time.timeScale = 1;
    }

    if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.JoystickButton0)) {
    Jump ();
    }
    // animator
    float animationSpeedPercent = ((running) ? currentSpeed.magnitude / runSpeed : currentSpeed.magnitude / walkSpeed * .5f);


    void Move(Vector2 inputDir, bool running) {


    float targetRotation = cameraT.eulerAngles.y;
    transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));


    Vector2 targetSpeed = new Vector2(
    ((running) ? runSpeed : walkSpeed) * inputDir.normalized.x,
    ((running) ? runSpeed : walkSpeed) * inputDir.normalized.y);

    currentSpeed = Vector2.SmoothDamp(currentSpeed, targetSpeed,
    ref speedSmoothVelocity,
    GetModifiedSmoothTime(speedSmoothTime));

    velocityY += Time.deltaTime * gravity;
    Vector3 velocity = (transform.forward * currentSpeed.y) +
    (transform.right * currentSpeed.x) +
    Vector3.up * velocityY;

    controller.Move(velocity * Time.deltaTime);
    currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z);

    if (controller.isGrounded)
    {
    velocityY = 0;
    }
    }


    I expected the script to turn the player via Mouse input and move him via the keyboard. Of course i did not expect him to slow down if he faces an other direction. Looking forward to your help.



    -nailuj05










    share|improve this question



























      0












      0








      0








      I added a strafing mechanic to my Player with the help of the stackoverflow community and since then I have a weird bug:



      When I rotate the Player (via Mouse input) towards the negative z direction my Player slows down (walking or running slows down massively).



      Before I added the strafing mechanic the Player was controlled via W,A,S,D and the Mouse, the mouse rotates the Player, W is walking forward and A,S,D turned the Player to the left, back or right and them walked in this direction.



      Since I added the strafing mechanic my Player could just walk, run and strafe while looking in the z direction. As soon as you turn around the whole Movement slows down.



      void Update () {
      // input
      Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
      Vector2 inputDir = input.normalized;
      bool running = Input.GetKey (KeyCode.LeftShift);

      if (!Input.GetMouseButtonDown(0) || Input.GetKey(KeyCode.JoystickButton2)) {
      Move (inputDir, running);
      Time.timeScale = 1;
      }

      if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.JoystickButton0)) {
      Jump ();
      }
      // animator
      float animationSpeedPercent = ((running) ? currentSpeed.magnitude / runSpeed : currentSpeed.magnitude / walkSpeed * .5f);


      void Move(Vector2 inputDir, bool running) {


      float targetRotation = cameraT.eulerAngles.y;
      transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));


      Vector2 targetSpeed = new Vector2(
      ((running) ? runSpeed : walkSpeed) * inputDir.normalized.x,
      ((running) ? runSpeed : walkSpeed) * inputDir.normalized.y);

      currentSpeed = Vector2.SmoothDamp(currentSpeed, targetSpeed,
      ref speedSmoothVelocity,
      GetModifiedSmoothTime(speedSmoothTime));

      velocityY += Time.deltaTime * gravity;
      Vector3 velocity = (transform.forward * currentSpeed.y) +
      (transform.right * currentSpeed.x) +
      Vector3.up * velocityY;

      controller.Move(velocity * Time.deltaTime);
      currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z);

      if (controller.isGrounded)
      {
      velocityY = 0;
      }
      }


      I expected the script to turn the player via Mouse input and move him via the keyboard. Of course i did not expect him to slow down if he faces an other direction. Looking forward to your help.



      -nailuj05










      share|improve this question
















      I added a strafing mechanic to my Player with the help of the stackoverflow community and since then I have a weird bug:



      When I rotate the Player (via Mouse input) towards the negative z direction my Player slows down (walking or running slows down massively).



      Before I added the strafing mechanic the Player was controlled via W,A,S,D and the Mouse, the mouse rotates the Player, W is walking forward and A,S,D turned the Player to the left, back or right and them walked in this direction.



      Since I added the strafing mechanic my Player could just walk, run and strafe while looking in the z direction. As soon as you turn around the whole Movement slows down.



      void Update () {
      // input
      Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
      Vector2 inputDir = input.normalized;
      bool running = Input.GetKey (KeyCode.LeftShift);

      if (!Input.GetMouseButtonDown(0) || Input.GetKey(KeyCode.JoystickButton2)) {
      Move (inputDir, running);
      Time.timeScale = 1;
      }

      if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.JoystickButton0)) {
      Jump ();
      }
      // animator
      float animationSpeedPercent = ((running) ? currentSpeed.magnitude / runSpeed : currentSpeed.magnitude / walkSpeed * .5f);


      void Move(Vector2 inputDir, bool running) {


      float targetRotation = cameraT.eulerAngles.y;
      transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));


      Vector2 targetSpeed = new Vector2(
      ((running) ? runSpeed : walkSpeed) * inputDir.normalized.x,
      ((running) ? runSpeed : walkSpeed) * inputDir.normalized.y);

      currentSpeed = Vector2.SmoothDamp(currentSpeed, targetSpeed,
      ref speedSmoothVelocity,
      GetModifiedSmoothTime(speedSmoothTime));

      velocityY += Time.deltaTime * gravity;
      Vector3 velocity = (transform.forward * currentSpeed.y) +
      (transform.right * currentSpeed.x) +
      Vector3.up * velocityY;

      controller.Move(velocity * Time.deltaTime);
      currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z);

      if (controller.isGrounded)
      {
      velocityY = 0;
      }
      }


      I expected the script to turn the player via Mouse input and move him via the keyboard. Of course i did not expect him to slow down if he faces an other direction. Looking forward to your help.



      -nailuj05







      c# unity3d 3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 14:27









      shizhen

      3,20041232




      3,20041232










      asked Jan 20 at 11:59









      nailuj05nailuj05

      83




      83
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276191%2funity-player-slows-down-when-looking-in-certain-direction%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276191%2funity-player-slows-down-when-looking-in-certain-direction%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage