• 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 /
avatar image
0
Question by Thorwan · Sep 27, 2016 at 10:41 AM · 2dlookattop-downwaypoint system

Waypoint navigation in 2D without LookAt

Hello!
I've started working on my first Unity project and could use some help. It's a top-down 2D game not unlike the UFO tutorial with enemies that should be moving between waypoints I specify with empty gameobjects per scene.

The following code worked well in an early prototype I made inside the the Roll-a-Ball Tutorial w$$anonymous$$ch is 3D, but doesn't work in my actual 2D game:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy2AI : MonoBehaviour
 {
     public GameObject[] waypoints;
     public int num = 0;
 
     public float minDist;
     public float speed;
 
     // Update is called once per frame
     void Update()
     {
         float dist = Vector3.Distance(gameObject.transform.position, waypoints[num].transform.position);
 
             if (dist > minDist)
             {
                 Move();
             }
             else
             {
                 if (num + 1 == waypoints.Length)
                 {
                     num = 0;
                 }
                 else
                 {
                 num++;
                 }
         }
     }
 
     public void Move()
     {
        gameObject.transform.LookAt(waypoints[num].transform.position);
        gameObject.transform.position += gameObject.transform.forward * speed * Time.deltaTime;
     }
 }
 

Reading various Unity Answers posts it seems like a lot of people seem to have trouble transitioning from 3D to 2D because LookAt doesn't work as expected. I've already tried a lot of the suggested workarounds, but couldn't get any of them to work for my specific case.

I'd really appreciate if someone could help me recreate the behavior of my script in 2D by suggesting a different form of waypoint system or providing an alternative way to ac$$anonymous$$eve what LookAt does for 3D.

Thanks in advance!

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

2 Replies

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

Answer by juicyz · Sep 28, 2016 at 12:17 AM

You could do what LookAt does. Use math and find the needed rotation between you and the next waypoint then rotate your transform based on that.

There are a lot of answers for t$$anonymous$$s already. Search the forums, answers, or google. http://answers.unity3d.com/questions/585035/lookat-2d-equivalent-.html Personally I like, transform.right = target.position - transform.position; Use the right transform.right/up/down/forward etc for your needs.

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
avatar image
0

Answer by kuivalappa · Mar 17, 2017 at 12:15 PM

try t$$anonymous$$s one

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

public class EnemyWaypoints : MonoBehaviour {

 public List<Transform> waypoints;
 private Transform currentWaypoint;

 public float speed = 5f;

 private float closeEnouth = 0.5f;
 int point = 0;
 


 void Start()
 {

     currentWaypoint = waypoints[point];
 }


 // Update is called once per frame
 void Update()
 {

     Quaternion rotation = Quaternion.LookRotation(waypoints[point].position - transform.position, transform.TransformDirection(Vector3.forward));
     transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);

     float dist = Vector3.Distance(waypoints[point].position, transform.position);
     transform.position = Vector3.MoveTowards(transform.position, waypoints[point].position, Time.deltaTime * speed);

     if (Vector3.Distance(t$$anonymous$$s.transform.position, waypoints[point].position) < closeEnouth)
     {
         if (point + 1 < waypoints.Count)
             point++;
      

     }
 }

}

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How do I make an object look at an another object in Top-Down 2D? ( C# ) 1 Answer

How To Use Fractals For 2D Procedural Generation 0 Answers

Keep player inside a non-square/irregular shape area 1 Answer

2D Animation does not start 1 Answer

transform.up flips gameobject by it's x-axis when transform.up.x is 0 and when transform.up.y is negative 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