- Home /
Question by
bobrovapaula · Dec 30, 2018 at 08:36 PM ·
animation2dsidescroller
What might me wrong with the "RightHook" animation? It doesn't work...
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour
{
public Transform target;
public float speed = 0.01f;
public float AttackDistance = 0.5f;
private float minDistance = 0.2f;
private float range;
private Rigidbody myRigidbody;
private float attackTimeCounter;
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody>();
}
void Update()
{
range = Vector2.Distance(transform.position, target.position);
if (range > minDistance)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
if (range < minDistance)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, -6 * speed * Time.deltaTime);
}
if (target.position.x > transform.position.x)
{
//face right
transform.localScale = new Vector3(1, 1, 1);
}
else if (target.position.x < transform.position.x)
{
//face left
transform.localScale = new Vector3(-1, 1, 1);
}
//walk y
if (this.transform.position.y > 0)
{
anim.SetBool("StartWalk", true);
//anim.Play("StartWalk");
}
//walk x
if (this.transform.position.x > 0)
{
anim.SetBool("StartWalk", true);
//anim.Play("StartWalk");
}
if (Vector3.Distance(transform.position, target.position) > AttackDistance)
{
anim.SetTrigger("LeftHook");
}
else
{
anim.SetBool("StartWalk", true);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Best new 2d tool to make a game like Zombieville ? 2 Answers
Need tips on simplest way to animate a simple 2d Character? 1 Answer
Fitting in a turn around animation (Alucard-like) in a transition from facing right to the left (2D) 1 Answer
2D Animation does not start 1 Answer
Zombie Cafe style game. 1 Answer