The question is answered, right answer was accepted
Play walk animation, based on object direction
Hello I've been at my computer for hours on end, trying to set a bool true in the animator based on the player movement direction.
bools: WalkForward WalkLeft WalkRight WalkBackwards
I'm doing this for mobile so WASD won't really help, I can't find anything at all to help me, everything I've seen and tried with Vector3 just gets the player's movement, based on world space. Not the movement direction the actual player object itself is moving in. I really need help with this, I can't sit here any longer trying to find answers :(
I have a script in C# but I move forward, depending on the direction I'm facing, both the forward and left bool will enable. Here is my code, I greatly appreciate any help I can get at all.
public class MoveDir : MonoBehaviour {
public Transform Player;
Vector3 oldPos;
Quaternion oldRot;
public bool isMovingForward;
public bool isMovingLeft;
public bool isMovingRight;
public bool isMovingBackwards;
void Update()
{
Vector3 movement = oldRot * (transform.position - oldPos);
oldPos = Player.InverseTransformPoint(transform.position);
if (movement.z > 0)
{
isMovingForward = true;
isMovingBackwards = false;
}
else if (movement.z < 0)
{
isMovingBackwards = true;
isMovingForward = false;
}
if (movement.z == 0)
{
isMovingBackwards = false;
isMovingForward = false;
}
if (movement.x > 0)
{
isMovingRight = true;
isMovingLeft = false;
}
else if (movement.x < 0)
{
isMovingLeft = true;
isMovingRight = false;
}
if (movement.x == 0)
{
isMovingLeft = false;
isMovingRight = false;
}
oldPos = Player.transform.position;
oldRot = Player.transform.rotation;
}
}
Answer by naiemali1171987 · Oct 19, 2020 at 06:43 PM
Sorry for the bump, but can someone please help? These unity forums are starting to become dead. Almost no one helps anymore
Answer by hendryshaikh2004 · Oct 19, 2020 at 07:18 PM
That's easy you have to make an animator component like this
animator anim;
anim = GetComponent<Animator>(); //in the start
anim.Setbool(true, "Name"); //Name of the bool animation to play
that's it. if it helps please give this message alike and sorry I just saw it now or would have replied back then.
Ohh no problem. However this isn't what I meant, the vector3 and position stuff bases it all off what direction it's moving in world space, not the direction the player is moving on it's transform. I know how to get the animator bools and things to work, it's just the position and vector3 stuff
Follow this Question
Related Questions
How to rotate the player towards a Vector2? 1 Answer
When I run this code Unity crashes(ANIMATOR RELATED) 0 Answers
Animation returns to the initial position 0 Answers
HELP!! How to make my Mixamo Character sit on a CHAIR?? A* Pathfinding + Mixamo Animation 0 Answers
Animator Is Not A Playable Error. 1 Answer