• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 04, 2016 at 06:11 PM by kunald.
avatar image
0
Question by kunald · Dec 04, 2016 at 08:28 AM · aiunity5spritesvehicleai problems

Unity2D Check which direction the enemy AI is heading?

Hey I need assistance figuring out which way my AI is going so I can rotate the vehicle if needed.

I tired to us xDir and yDir. The xDir script works great but the yDir script is funky and making the vehicle rotate when going in the x direction.

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class AICarScript : MonoBehaviour {
 public Transform[] waypoints;
 int cur = 0;
 
 public float speed = 0.3f;
 
     // Use this for initialization
     void Start () {
     
     }
     void CheckDirection()
     {
     //Declare x and y movement variables. Ranges from -1 to 1
     int xDir = 0;
     int yDir = 0;
     yDir = waypoints[cur].position.y > transform.position.y ? 1 : -1;
     xDir = waypoints[cur].position.x > transform.position.x ? 1 : -1;
 
         //Check if the car is going left or right
             if (xDir == 1)
             {
             GetComponent<SpriteRenderer>().flipX = true;
             //GetComponent<Rigidbody2D>().rotation = 0;
 
 
             }
             else if (xDir == -1)
             {
             GetComponent<SpriteRenderer>().flipX = false;
             //GetComponent<Rigidbody2D>().rotation = 0;
 
             }
 
         //Check if the car is going up and down
         if (yDir == 1)
         {
         GetComponent<Rigidbody2D>().rotation = 90;
         }
         else if (yDir == -1)
         {
         GetComponent<Rigidbody2D>().rotation = -90;
         }
         else if (yDir == 0)
         {
         GetComponent<Rigidbody2D>().rotation = 0;
         }
 
 
     }
     // Update is called once per frame
     void Update () {
 
     //Waypoint not reached yet? Move towards it
     if (transform.position != waypoints[cur].position)
     {
     Vector2 p = Vector2.MoveTowards(transform.position, waypoints[cur].position, speed);
     GetComponent<Rigidbody2D>().MovePosition(p);
     CheckDirection();
 
     }
     ///Waypoint reached
     else cur = (cur + 1) % waypoints.Length;
 
     }
     bool valid(Vector2 dir)
     {
     Vector2 pos = transform.position;
     RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
     return (hit.collider == GetComponent<Collider2D>());
     }
 
 
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by fafase · Dec 04, 2016 at 09:33 AM

Since you are using the rigidbody you can look at

     rigidbody.velocity;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemies Follow Player2 0 Answers

Making an AI Flee 2 Answers

Steering an AI vehicle to a waypoint using float of -1 to 1? 1 Answer

Follow AI goes up when it gets near me! 0 Answers

Nav Mesh Agent 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges