0

The following code supposes to move and rotate the character based on the plyer's input. However, it works perfectly when moving forward, left, and right, but keeps spinning when moving backward.

Does anyone know why that is? I really can't figure out how is that possible, it uses exactly the same code with moving toward other directions, why it just doesn't work when it comes to moving backward?!

Please help me! I'm losing my sanity over this.

The code is placed inside Update() function.

PS: So, now I am certain the problem is that the if statement for moving backward is always triggered. I logged transform.foward when moving backward, it outputs (0,0,-1) but the Equals function still returns false. I don't understand why is that, and why this does not happen to any other directions. Please help, thank you!

PSS: So I switched .Equals function to == operation, and it works now. Still curious about why this happens though.

// Player Movements
        hInput = Input.GetAxis("Horizontal");
        vInput = Input.GetAxis("Vertical");
        if(Mathf.Abs(hInput) > Mathf.Abs(vInput))
        {
            if(hInput > 0)
            {
                // move right
                if (!transform.forward.Equals(new Vector3(1, 0, 0)))
                {
                    transform.Rotate(90, 0, 0);

                }
            } else if(hInput < 0)
            {
                // move left
                if (!transform.forward.Equals(new Vector3(-1, 0, 0)))
                {
                    transform.Rotate(-90, 0, 0);
                }
            }
            controller.Move(new Vector3(hInput, 0, 0) * mSpeed * Time.deltaTime);
        } else if(Mathf.Abs(hInput) < Mathf.Abs(vInput))
        {
            if (vInput > 0)
            {
                // move forward
                if (!transform.forward.Equals(new Vector3(0, 0, 1)))
                {
                    transform.Rotate(90, 0, 0);

                }
            }
            else if (vInput < 0)
            {
                // move backward
                if (!transform.forward.Equals(new Vector3(0, 0, -1)))
                {
                    transform.Rotate(-90, 0, 0);
                }
            }

            controller.Move(new Vector3(0, 0, vInput) * mSpeed * Time.deltaTime);
        }strong text
  • The problem is when you check if (!transform.forward.Equals(new Vector3(0,0,-1))) its always false, add a debug before this line and see what transform.forward is going to – vasmos Aug 11 '20 at 15:48
  • Yes, what's weird is when I log the transform.foward, it outputs exactly (0,0,-1) but keeps on rotating as if the statement is false. – Tianbo Zhao Aug 11 '20 at 16:13

1 Answers1

0

What components do you have on the player? Also, I would recommend logging the transform.foward function

UnityEngine.Debug.Log(transform.forward)

as it seems that transform.foward is the mostly likely cause for your issue.

ChilliPenguin
  • 710
  • 7
  • 14
  • Thanks for the reply! I logged the transform.forward, it outputs correctly, it outputs (0,0,-1), as well as the other three directional vectors. However, it just doesn't stop rotating as it does for the other three directions. The player only has a CharacterController and a CapsuleCollider, nothing else. – Tianbo Zhao Aug 11 '20 at 16:12
  • That's weird, try placing the debug log in the else if(vInput < 0) to see if that if statement is executing correctly, and maybe try logging the vInput if that does not log itself. – ChilliPenguin Aug 11 '20 at 16:22
  • I checked, vInput < 0 part is good. The problem is definitely on the Equals function. It all works now after I switched to == operation. – Tianbo Zhao Aug 11 '20 at 18:23
  • Huh, did not know that .Equal and == were different. Looked into it and found [this](https://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals), but its still weird that you first three movements worked. – ChilliPenguin Aug 11 '20 at 18:59