Movement works in all directions except one?
I have a player who can move up, down, left and right (which all function) but created another blend tree with movements so that when the player stops moving it faces the direction it was last moving in. This works for moving up (shown here ) and right but after moving left and down the blend tree quickly switches from the last move to the original face down pose as shown here after moving left. Any idea what's going on?
Also my sword works and is in place as shown in my animation when moving up and right but not for left and down?
My Player Controller code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private Animator anim;
private Rigidbody2D myRigidbody;
private bool playerMoving;
public Vector2 lastMove;
private static bool playerExists;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
if (!playerExists)
{
playerExists = true;
DontDestroyOnLoad(transform.gameObject);
}
else
{
Destroy(gameObject);
}
}
// Update is called once per frame
void Update()
{
playerMoving = false;
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxis("Horizontal") < -0.5f)
{
//transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidbody.velocity.y);
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxis("Vertical") < -0.5f)
{
//transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed);
playerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
{
myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("PlayerMoving", playerMoving);
anim.SetFloat("LastMoveX", lastMove.x);
anim.SetFloat("LastMoveY", lastMove.y);
}
}
Your answer
Follow this Question
Related Questions
Script to animation and movement of 2d player 1 Answer
Adding small acceleration to 2d top down movement game? 0 Answers
Object reference not set to an instance of an object (Unity 2D animation) 1 Answer
Why won't my 2D Sprite Move? 1 Answer
Spawned game object does not carry the Animator component 0 Answers