i’ve recently started to learn coding alongside this project to make a top-down rpg,and i am having some trouble assigning my walking and idling animations to my character.
first off i have used the following code to make the movement so i can make the movement grid based and disable diagonals and disable moving to a collider
using System.Collections;
using UnityEngine;
class Movementt : MonoBehaviour {
public float moveSpeed = 3f;
public float runSpeed = 6f;
private float gridSize = 1f;
private bool allowDiagonals = false;
private bool correctDiagonalSpeed = true;
private Vector2 input;
private bool isMoving = false;
private Vector2 startPosition;
private Vector2 endPosition;
private float t;
private float factor;
public void Start ()
{
}
public void FixedUpdate() {
if (!isMoving) {
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (!allowDiagonals) {
if (Mathf.Abs(input.x) > Mathf.Abs(input.y)) {
input.y = 0;
} else {
input.x = 0;
}
}
if (input != Vector2.zero) {
StartCoroutine(move(transform));
}
}
}
public IEnumerator move(Transform transform) {
isMoving = true;
t = 0;
if(allowDiagonals && correctDiagonalSpeed && input.x != 0 && input.y != 0) {
factor = 0.7071f;
} else {
factor = 1f;
}
startPosition = rigidbody2D.transform.position;
endPosition = new Vector2 (startPosition.x + System.Math.Sign (input.x) * gridSize,
startPosition.y + System.Math.Sign (input.y) * gridSize);
RaycastHit2D right = Physics2D.Raycast (startPosition, Vector2.right, 1f);
RaycastHit2D left = Physics2D.Raycast (startPosition, -Vector2.right, 1f);
RaycastHit2D up = Physics2D.Raycast (startPosition, Vector2.up, 1f);
RaycastHit2D down = Physics2D.Raycast (startPosition, -Vector2.up, 1f);
if (right.collider != null && endPosition.x > startPosition.x) {
endPosition = startPosition;
}
if (left.collider != null && endPosition.x < startPosition.x) {
endPosition = startPosition;
}
if (up.collider != null && endPosition.y > startPosition.y) {
endPosition = startPosition;
}
if (down.collider != null && endPosition.y < startPosition.y) {
endPosition = startPosition;
}
while (t < 1f)
{
if (Input.GetKey (KeyCode.LeftShift)) {
t += Time.deltaTime * (runSpeed / gridSize) * factor;
}
else {
t += Time.deltaTime * (moveSpeed/gridSize) * factor;
}
transform.position = Vector2.Lerp(startPosition, endPosition, t);
yield return null;
}
isMoving = false;
yield return 0;
}
}
but since my characters is 2 units long and i want it to raycast from the feet i added the character as a child entity with y=1,and added the following code to try and animate it
using UnityEngine;
using System.Collections;
public class playeranimation : MonoBehaviour {
Animator anim;
private Vector2 input;
// Use this for initialization
private float compareX;
private float compareY;
void Start () {
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void fixedUpdate () {
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (Mathf.Abs (input.x) > Mathf.Abs (input.y)) {
input.y = 0;
} else {
input.x = 0;
}
compareX = input.x;
compareY = input.y;
if (compareX > 0) {
if (Input.GetKey (KeyCode.LeftShift)) {
anim.SetFloat ("MoveDirection", 2);
} else {
anim.SetFloat ("MoveDirection", 1);
}
anim.SetFloat ("IdleDirection", 2f);
}
if (compareX < 0) {
if (Input.GetKey (KeyCode.LeftShift)) {
anim.SetFloat ("MoveDirection", -2);
} else {
anim.SetFloat ("MoveDirection", -1);
}
anim.SetFloat ("IdleDirection", 3f);
}
if (compareY > 0) {
if (Input.GetKey (KeyCode.LeftShift)) {
anim.SetFloat ("MoveDirection", 4);
} else {
anim.SetFloat ("MoveDirection", 3);
}
anim.SetFloat ("IdleDirection", 0f);
}
if (compareY < 0) {
if (Input.GetKey (KeyCode.LeftShift)) {
anim.SetFloat ("MoveDirection", -4);
} else {
anim.SetFloat ("MoveDirection", -3);
}
anim.SetFloat ("IdleDirection", 1f);
}
if (input == Vector2.zero) {
anim.SetFloat ("Movedirection", 0);
}
}
}
and although i set these values to the animations none play whatsoever
updated the question with a bit more details
– Davenger