What direction is the gameObject going?

Hi :slight_smile:
I want my gameObject to change animation, changing with the direction that is going.
The world 3d with objects 2d
I’m trying with velocity unsuccessfully :frowning:
is working:
The monster follows the target until it gets close.
help me
my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class controleWyvern : MonoBehaviour
{
    public Animator wyvern;
    public Rigidbody myBody;
    Transform alvo;
    float velocidade = 5f, distancia = 20;
    bool alvoEncontrado = false;

    // Use this for initialization
    void Start()
    {
        alvo = GameObject.FindGameObjectWithTag("Player").transform;
        myBody = GetComponent<Rigidbody>();
  

    }

    // Update is called once per frame
    void Update()
    {
        if (Vector3.Distance(transform.position, alvo.position) > distancia)
        {
            alvoEncontrado = false;
        }
        else
        {
            alvoEncontrado = true;
            velocidade = 5;
            if (distancia / 2 > Vector3.Distance(transform.position, alvo.position))
            {
                velocidade = 0;
            }
        }
        if (alvoEncontrado)
        { 
   
            transform.position = Vector3.MoveTowards(transform.position, alvo.position, velocidade*Time.deltaTime);
        }

//here is not working \/\/\/\/\/
        if (myBody.velocity.z > 0)
        {
            wyvern.SetBool ("runU", true);
            wyvern.SetBool ("runD", false);
            wyvern.SetBool ("runL", false);
            wyvern.SetBool ("runR", false);
        }
        if(myBody.velocity.z < 0)
        {
          
            wyvern.SetBool ("runU", false);
            wyvern.SetBool ("runD", true);
            wyvern.SetBool ("runL", false);
            wyvern.SetBool ("runR", false);
        }
        if (myBody.velocity.x > 0)
        {
            wyvern.SetBool ("runU", false);
            wyvern.SetBool ("runD", false);
            wyvern.SetBool ("runL", true);
            wyvern.SetBool ("runR", false);
        }
        if(myBody.velocity.x < 0)
        {
            wyvern.SetBool ("runU", false);
            wyvern.SetBool ("runD", false);
            wyvern.SetBool ("runL", false);
            wyvern.SetBool ("runR", true);
        }
    }
}

PLEASE use code tags, that code is near impossible to read: Using code tags properly - Unity Engine - Unity Discussions

There’s 2 ways I see of going at it.

1: (Easiest) Get the directions based on input. For example, if you’re pressing A or D, your player is going left or right.
B: (Slightly harder) Often check the difference in positions of the player. Like this:

Vector 3 PrevLocation;
Vector3 Difference;

void Update()
{
    Difference = Player.transform.position - PrevLocation;
    PrevLocation = transform.position
}

That code does it every frame, but you could also use a Coroutine to check a few times a second.

1 Like

first option:
Will not work correctly, because it will not have input and the monster can continue following the player

second option:
How do I use this in a condition to change status to true or false?

wyvern.SetBool ("runU", true);

or

wyvern.SetBool ("runU", false);

I’m a beginner
ty :slight_smile:

Well I’m not sure exactly what your code is, but for example:

if(Difference.x <0)
{
    Debug.Log("Moved left");
}

if(Difference.x > 0)
{
    Debug.Log("Moved right");
}

And so on.

There’s probably a smarter way of doing it, but that’s what I know.

1 Like

this working
THANKS BRO

1 Like

I know it’s been a while since this post, but this has worked so well for me. Thank you!