最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Enemy 3d Follow Player - play animation in function of direction unity 3d? - Stack Overflow

programmeradmin0浏览0评论

I am currently working on a 2.5d game. I have sprites in a 3d space. It is a kind of football game. I want that when an enemy follows my player that he plays an animation depending on the direction he is going.

I used the transform.position.x method but that does not work well because there is no input. I would like to know how to play an animation where the enemy runs from behind when he is going backwards, from right when he is going right, left when he is going left, back-left when he is moving from back to left. Without going through the blend tree if possible.

My code :

using UnityEngine;

public class EnnemyAnimation : MonoBehaviour
{
    void Update()
    {
       if (transform.position.x > 0)
       {
           Debug.Log("right");   // play animation right
       }
       else if (transform.position.x < 0)
       {
           Debug.Log("left");    // play animation left
       }
       else if (transform.position.z < 0) 
       {
           Debug.Log("back");    // play animation back
       }
       else if (transform.position.z > 0)
       {
           Debug.Log("front");    // play animation front
       }
       else if (transform.position.x < 0 && transform.position.z < 0)
       {
           Debug.Log("back-left");    // play animation back left
       }
       else if (transform.position.x > 0 && transform.position.z < 0)
       {
           Debug.Log("back-right");   // play animation back right
       }
       else if (transform.position.x < 0 && transform.position.z > 0)
       {
           Debug.Log("front-left");    // play animation front left
       }
       else if (transform.position.x > 0 && transform.position.z > 0)
       {
           Debug.Log("front-right");   // play animation front right
       }
    }
}

I am currently working on a 2.5d game. I have sprites in a 3d space. It is a kind of football game. I want that when an enemy follows my player that he plays an animation depending on the direction he is going.

I used the transform.position.x method but that does not work well because there is no input. I would like to know how to play an animation where the enemy runs from behind when he is going backwards, from right when he is going right, left when he is going left, back-left when he is moving from back to left. Without going through the blend tree if possible.

My code :

using UnityEngine;

public class EnnemyAnimation : MonoBehaviour
{
    void Update()
    {
       if (transform.position.x > 0)
       {
           Debug.Log("right");   // play animation right
       }
       else if (transform.position.x < 0)
       {
           Debug.Log("left");    // play animation left
       }
       else if (transform.position.z < 0) 
       {
           Debug.Log("back");    // play animation back
       }
       else if (transform.position.z > 0)
       {
           Debug.Log("front");    // play animation front
       }
       else if (transform.position.x < 0 && transform.position.z < 0)
       {
           Debug.Log("back-left");    // play animation back left
       }
       else if (transform.position.x > 0 && transform.position.z < 0)
       {
           Debug.Log("back-right");   // play animation back right
       }
       else if (transform.position.x < 0 && transform.position.z > 0)
       {
           Debug.Log("front-left");    // play animation front left
       }
       else if (transform.position.x > 0 && transform.position.z > 0)
       {
           Debug.Log("front-right");   // play animation front right
       }
    }
}
Share Improve this question edited Feb 3 at 11:13 MikaMikashu asked Feb 2 at 16:58 MikaMikashuMikaMikashu 576 bronze badges 3
  • 1 You're only checking for positions .. if you want to play animations according to movement you'd rather have to go accordingly by the movement instead of the current position... – derHugo Commented Feb 2 at 17:12
  • I tried with movement.x<0 or movement.z<0 and a Debug.Log() but no message appears in the console of Unity. The problem is there is no input, because it's an a.i ennemy. It's not controlled by the player – MikaMikashu Commented Feb 2 at 19:08
  • 1 well .. something is moving it so you gotta track that movement ... for instance you could keep track of the previous position ever frame and then compare to the current one – derHugo Commented Feb 3 at 7:31
Add a comment  | 

1 Answer 1

Reset to default 1

Since your enemies are following the player, you can find which direction they are going by substracting Player's position from Enemy's position. Then you can decide which animation to play just like you did on your if-else statements.

Vector2 facingDirection = new Vector2(player.transform.position.x - enemy.transform.position.x,
     player.transform.position.z - enemy.transform.position.z);

if(facingDirection.x > 0 && facingDirection.y > 0) {
   ...
}
if else(facingDirection.x < 0 && facingDirection.y) {
   ...
}
...

We used transform.x and transform.z of player and enemy because your game is in 3d world and we dont want to go up (y axis is up).

If you didnt understood why we substracted positions I recommend you this video

https://www.youtube/watch?v=sYf4bSj9j2w

发布评论

评论列表(0)

  1. 暂无评论