• 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
1
Question by Orzel · Feb 25, 2014 at 08:21 PM · rotationmovementdirection

Move into direction of another object

Hello guys.

I want to move my object in the direction of another object. I tried this one:

 moveDir = new Vector3(target) - new Vector3(actualPos);
 moveDir = moveDir.normalized;
 gameObject.transform.position += moveDir;

And it seems to work fine, but only if my object is directed forward (rotation 0,0,0). When it's directed backward (rotation 0,180,0) it doesn't work.

How can I do this? Maybe some easier method but everything will be useful. Bests, Marshall.

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
2

Answer by robertbu · Feb 25, 2014 at 08:42 PM

Nothing is this code should be impacted by the direction your character is facing assuming that 'target' and 'acutalPos' are being set correctly. Note if this is executed in Update(), you should be using Time.deltaTime. An alternate code fragment (I'm assuming C# since you are using the 'new' operator):

 Vector3 moveDir = (target.position - transform.position).normalized;
 transform.position += moveDir * speed * Time.deltaTime;

Note here that 'target' is the transform of the target object. Typically you would declare it at the top of the file like (C#):

  public Transform target;

And you would initialize it either by dragging and dropping in the Inspector, or by using GameObject.Find() in start.

Comment
Add comment · Show 3 · 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 Orzel · Feb 26, 2014 at 06:58 AM 0
Share

I'm not using Time.deltaTime cuz it's one frame of move it.

 function Update() {
   if(something)
     $$anonymous$$oveIt();
 }
 function $$anonymous$$oveIt() {
   var moveDir : Vector3 = (target.position - transform.position).normalized;
   transform.position += moveDir;
 }

$$anonymous$$y variables "target" and "actualPos" were only examples. So problem is still need help to solve this problem. I'm little sure that I have to count something with transform.rotation main object which I want to move, but don't have any idea, how to count it...

 if(rotation.y == 0)then O$$anonymous$$
 else NOT O$$anonymous$$

I think that I should equal rotation.y to zero, but only in counting, not in graphic.

avatar image robertbu · Feb 26, 2014 at 07:03 AM 0
Share

There is a lot of room for misunderstanding when you use code with 'examples' or placeholders. Let's use a concrete script that we both can talk about to see where you have a problem:

 #pragma strict
 
 var target : Transform;
 var moveDist = 0.75;
 
 function Update() {
     if (Input.Get$$anonymous$$ouseButtonDown(0)) {
         var dir = target.position - transform.position;
         if (dir.magnitude <= moveDist) {
             transform.position = target.position;
         }
         else {
             transform.position += dir.normalized * dist;
         }
     }
 }

It is expected that 'target' is initialized by dragging and dropping the game object to be used as a target on the 'target' variable in the Inspector.

This code will move a maximum of moveDist units towards the target each time the left mouse button is clicked. Rotation does not matter. Start a new scene, crate a cube, attach this script, create a target game object, drag and drop the target onto 'target' in the inspector, hit play and click the left mouse button.

avatar image Wrimor · Jun 13, 2019 at 03:41 AM 0
Share

This is super old, but thank you so much! I just spent an hour or so searching the proper way to move an object in the direction of another object and this is it. No complicated code that I had. No need to detect the direction or rotation. Fantastic answer!

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

21 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

Related Questions

How to make an object go the direction it is facing? 6 Answers

How to make a movement system that changes with your viewpoint but also lets you hit multiple keys at once to go diagonally? 0 Answers

How do I keep my character facing the direction of travel after movement stops? 1 Answer

Help with my Character Controller 1 Answer

How to incorporate a rotation towards mouse position in this script? I tried 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