Hi, I've tried to get the enemy to move and receive the 2D animation, without rotation, but changing the sprites. I have a script for random moving and towards the Player, and another script for the animation. For the animator I am making several transitions, but it still does not work, for example "GusIdleRight -> GusRight", the enemy is pointed to the right if it receives movement, do the animation if it is left for example "GusIdleRight -> GusIdleLeft "To change direction and move like the previous one, but it does not work. Follows animator and sprite images, and scripts move and animation. Any tips or help? Grateful.
![alt text][1]
[1]: /storage/temp/84573-animator.png
MOVEMENT:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AiNovo : MonoBehaviour {
private Vector3 Player;
private Vector2 Playerdirection;
private Rigidbody2D myRigidbody;
public float speed;
private float distance;
private bool moving;
public float timeBetweenMove;
private float timeBetweenMoveCounter;
private float timeToMoveCounter;
public float timeToMove;
private Vector3 moveDirection;
public float waitToReload;
// Use this for initialization
void Start () {
myRigidbody = GetComponent ();
timeBetweenMoveCounter = Random.Range(timeBetweenMove * 1.25f, timeBetweenMove * 1.25f);
timeToMoveCounter = Random.Range(timeBetweenMove * 1.25f, timeBetweenMove * 1.25f);
}
// Update is called once per frame
void Update () {
distance = Vector2.Distance (Player, transform.position);
Player = GameObject.Find ("Player").transform.position;
if (distance < 7f && distance > 1.8f) {
myRigidbody.position = Vector3.MoveTowards (transform.position, Player, Time.deltaTime * speed);
}
else if (distance > 7f) {
randomMove ();
}
}
void randomMove () {
distance = Vector2.Distance (Player, transform.position);
if (distance > 7f) {
if (moving) {
timeToMoveCounter -= Time.deltaTime;
myRigidbody.velocity = moveDirection;
moving = true;
if (timeToMoveCounter < 0f) {
//timeBetweenMoveCounter = timeBetweenMove;
timeBetweenMoveCounter = Random.Range (timeBetweenMove * 1.25f, timeBetweenMove * 1.25f);
moving = false;
}
} else {
timeBetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = Vector2.zero;
if (timeBetweenMoveCounter < 0) {
//timeToMoveCounter = timeToMove;
timeToMoveCounter = Random.Range (timeBetweenMove * 1.25f, timeBetweenMove * 1.25f);
moveDirection = new Vector3 (Random.Range (-1f, 1f) * speed, Random.Range (-1f, 1f) * speed, 0f);
moving = true;
}
}
}
}
}
ANIMATION:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AnimMob : MonoBehaviour {
private Animator anim;
private Rigidbody2D myRigidbody;
// Use this for initialization
void Start () {
anim = GetComponent();
myRigidbody = GetComponent();
}
// Update is called once per frame
void Update () {
if (myRigidbody.velocity.x > 0) {
anim.SetBool ("Left", false);
anim.SetBool ("LeftRun", false);
anim.SetBool ("Right", true);
anim.SetBool ("RightRun", true);
}
if (myRigidbody.velocity.x < 0) {
anim.SetBool ("Left", true);
anim.SetBool ("LeftRun", true);
anim.SetBool ("Right", false);
anim.SetBool ("RightRun", false);
}
if (myRigidbody.velocity.x == 0) {
anim.SetBool ("LeftRun", false);
anim.SetBool ("RightRun", false);
}
if (myRigidbody.velocity.y > 0) {
anim.SetBool ("Down", false);
anim.SetBool ("DownRun", false);
anim.SetBool ("Up", true);
anim.SetBool ("UpRun", true);
}
if (myRigidbody.velocity.y < 0) {
anim.SetBool ("Down", true);
anim.SetBool ("DownRun", true);
anim.SetBool ("Up", false);
anim.SetBool ("UpRun", false);
}
if (myRigidbody.velocity.y == 0) {
anim.SetBool ("DownRun", false);
anim.SetBool ("UpRun", false);
}
}
}
↧