Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by kunald · Dec 02, 2016 at 09:57 PM · 2d gameaiunity5vector2

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 code works great but the yDir code 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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by studioAdam · Dec 02, 2016 at 10:28 PM

Hey @kunald,

You're just manually changing the y rotation of the AI car to be either 90, -90 or 0. You aren't += 90, just hard setting it to one of the three. Perhaps try checking out Transform.Lookat() in place of trying to manipulate the y with those values. You could do it every frame, but maybe just try doing it after you set the new waypoint so you only have to do it once.

gameObject.transform.LookAt(waypoints[cur].transform.position);

https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

I hope that helps.

Cheers.

Comment
Add comment · Show 2 · 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
avatar image kunald · Dec 02, 2016 at 10:53 PM 0
Share

Hey Adam,

Thanks for the reply. Unfortunately LookAt doesn't work properly with Vector2. It just rotates the sprite on it's Z axis. I tried a work around to stop it from rotating yet it still doesn't rotate on it's y axis when it goes down or up.

avatar image kunald kunald · Dec 03, 2016 at 12:37 AM 0
Share

I finally was able to figure it out by looking deeper into the LookAt function. Thanks so much for pointing me in the right direction! I would give you points but I have none. lol

Here is how I was able to get it to work. I added this code after I set a new waypoint:

 Quaternion rotation = Quaternion.LookRotation(waypoints[cur].transform.position - transform.position, transform.TransformDirection(Vector3.up));
         transform.rotation = new Quaternion(0, 0, rotation.x, rotation.w);

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3D Scrolling in 2D game 1 Answer

How do I turn off the previous object? 1 Answer

Enemy pathfinding script for a 2D top down shooter game 0 Answers

AI that doesn't use NavMesh and is similar to PacMan 1 Answer

Should i use big one scene, or divided scene for AI Navigation ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges