• 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 $$anonymous$$ · Jul 20, 2018 at 05:01 AM · 2ddirection

Translate in the direction of a point?

So if I have an NPC that needs to travel from point A to point B, and Point A is equal to 0,0, while Point B is equal to 10,50, then how do I get the NPC to go in the direction of point B, rather than go up ten and to the right fifty? I'm sure this is really simple, but my brain is not working right now and I usually just turn the NPC in the direction of its destination and use Transform.forward.

Comment
Add comment · Show 1
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 toddisarockstar · Jan 03, 2021 at 04:47 AM 1
Share

To get direction you just subtract two points. direction is that simple. then simply add it back to the enemy position, or set the velocity with it for movement.

 Vector3 direction = player.transform.position - enemy.transform.position;
         enemy.transform.position += direction.normalized*Time.deltaTime*speed;


1 Reply

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

Answer by The_Icaruz · Jul 20, 2018 at 05:16 AM

Hey, Unity has a usefull Gamekit where you could add nodes ("Waypoints") to which your character should move. You can just add nodes if you want more waypoints or a pattern your character should move. In their tutorial they are using a platform but I think that makes no difference for your use.

To the GameKit

EDIT :

Or if you want only a script for your NPC to move between points you could use this script :

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  public class MoveNPC : MonoBehaviour
  {
      public GameObject npc;
      public float moveSpeed;
      public Transform currentPoint;
      public Transform [] points;
      public int pointSelection;
 
 
      // Use this for initalization
      void Start()
      {
          currentPoint = points [pointSelection];
      }
 
      void Update()
      {
          npc.transform.position = Vector3.MoveTowards (npc.transform.position, currentPoint.position, Time.deltaTime * moveSpeed);
          if (npc.transform.position == currentPoint.position)
          {
              pointSelection++;
 
              if (pointSelection == points.Length)
              {
                      pointSelection = 0;
              }
 
              currentPoint = points[pointSelection];
          }
      }
 }

Now you just add childs to your NPC for each waypoint you want and add it to the points array.

Best regards, TheIcaruz

Comment
Add comment · Show 4 · 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 $$anonymous$$ · Jul 20, 2018 at 05:21 AM 0
Share

I just need to translate in the direction of the point B.

avatar image The_Icaruz $$anonymous$$ · Jul 20, 2018 at 05:36 AM 0
Share

Just updated my answer with a script where you can add as many waypoints you want, you also could add a delay between each points so the NPC stays for a deter$$anonymous$$ed time at the point and after that he moves to the next point.

If the script is not working let me know, I've got no time at the moment to try it myself but it is a simple script i had in my $$anonymous$$d when i read your question.

Hope this helps.

avatar image $$anonymous$$ · Jul 20, 2018 at 05:41 AM 0
Share

Thanks, all I needed was Vector3.$$anonymous$$oveTowards, which should work fine for what I'm doing.

avatar image The_Icaruz $$anonymous$$ · Jul 20, 2018 at 05:54 AM 0
Share

Oh okay, anyway glad i could help.

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

181 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 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

2D Box Collider / Sprite change direction issue 1 Answer

2D Shooting right only. Doesn't flip direction with character 1 Answer

Instantiate object that points to the direction 1 Answer

If player faces right, shoot right. viceversa? 1 Answer

Bullets direction is not good (network) 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