I have a simple code that moves my player but I cannot get it to animate properly. I have set up a parameter called speed and if speed is greater than 0.01 then it will animate walking. However I cannot integrate this into my code. Please help. Thanks in advance!
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Move : MonoBehaviour
{
private bool facingLeft = true;
private Rigidbody2D rb;
private Animator anim;
public float speed;
private float hold = 0f;
private float velocity;
void Start()
{
targetPosition = new Vector2(0.0f, 0.0f);
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
Vector3 targetPosition;
Vector3 theScale = new Vector3(1f, 1f, 1f);
void Update()
{
if (Input.GetMouseButtonDown(0))
{
targetPosition = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(targetPosition.x, targetPosition.y, 0.0f));
//moving to he right
if (targetPosition.x - this.transform.position.x > 0)
{
theScale.x = 1;
rb.transform.localScale = theScale;
hold = 1f;
}
//moving to the left
if (targetPosition.x - this.transform.position.x < 0)
{
theScale.x = -1;
rb.transform.localScale = theScale;
hold = 1f;
}
}
if (targetPosition.x - this.transform.position.x == 0)
{
hold = 0f;
}
anim.SetFloat("Speed", Mathf.Abs(hold));
this.transform.position = Vector2.MoveTowards(this.transform.position, targetPosition, speed * Time.deltaTime);
}
void reverseImage()
{
facingLeft = !facingLeft;
Vector2 theScale = rb.transform.localScale;
theScale.x *= -1;
rb.transform.localScale = theScale;
}
}