I Just followed brackeys animation tutorial, but after setting up the “Mathf.Abs” thing whenever I move right, my character faces to the left, he still moves right. And if I go left then he faces right but goes left.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
} else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
void FixedUpdate()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
Go back to the tutorial. You missed something. Mathf.Abs is working fine and its use here has nothing to do with which direction your character is facing.
Thanks for letting me know, after watching through both the animation and movement video, i have not found any issues or such, I even re-downloaded the Character Controller script to make sure nothing was changed there, and the weird part is, this only started happening after I added Math.Abs, but when I remove it it still does it
That happens all the time. You add “X”, the program starts to work funny, and you look for problems with “X”. But for real, most of the time there was a problem somewhere else that you either never noticed or never tested before. Sometimes you had 2 mistakes that cancelled out and you only noticed when X changed things up. Knowing that – really knowing “the mistake is obviously here” is almost always wrong – is one of the first steps of being a real computer programmer.
But that’s more of a joke for people who are already programmers, right? Conversations I had with students tend to be more like:
Why do you think the problem is there?
Because that;s the last thing I added
Was it working before?
Yes.
Let me try again. Was the exact thing you have a problem with now, working before?
Uh, I don’t know. I guess I didn’t check
What’s wrong with it?
It;s not working
But how is it not working? Is it always 10 times too big?
In this case the OP did the last part, which is pretty good. The movement is correct. And the animation isn’t stuck – it flips left/right when the keys are pressed. But it’s exactly backwards. I don’t see where they choose or “aim” the animation. The code sets the speed, using Abs so it’s always positive. Does that work? Does it run faster as you move faster?
Agreed, but perhaps others might learn to step back and stop tunnel-visioning.
Also, the six steps ties together the 2-wrongs-cancel-out, which is often the causative chain that leads you to the “Works fine on my machine!” and then you go down the next 5 steps as well.
Step 7 would be perhaps a month later while you’re driving to Las Vegas and you realize “Oh my gosh, it only worked the first time because of the blah blah blah problem…”
I love your simulated conversation… yeah, I’ve had those talks too, surprisingly even with some professional engineers. Today I work with a fantastic team of engineers and none of them would truly be stuck by anything like that, but we still all trade ideas off one another all the time and not a day goes by that you don’t hear, “Oh man, I didn’t even THINK it could be that thing!”
Humorously we have completely hijacked OP’s thread, but let me close this with a quick note to @LQPewds in case he has not yet solved the issue (which maybe is what his last post says?):
@LQPewds , if you are still banging on this, hang in there, you’ll find it. To help, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.