• 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 dirilikerhan355 · Mar 11, 2020 at 06:12 PM · mathfanglespositions

How To Calcutale New Position and Angle On The Line Objects

Hello to everyone. How can I position the object between two points so that when I move the points the object rests on the line. I made the angular calculation of the object, but I could not find what code to write about its position. please help me. 2 picture added commend. please look at pictures The codes I wrote for the angle:

     public GameObject node1, node2;

     Vector3 dir1 = (node1.transform.position - node2.transform.position);
     float atan2_1 = Mathf.Atan2(dir1.y, dir1.x);
     transform.rotation = Quaternion.Euler(0f, 0f, atan2_1 * Mathf.Rad2Deg);
Comment
Add comment · Show 3
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 dirilikerhan355 · Mar 11, 2020 at 06:19 PM 0
Share

alt text

question-1.png (16.4 kB)
question.png (11.9 kB)
avatar image dirilikerhan355 · Mar 11, 2020 at 06:44 PM 0
Share

alt text

question2.jpg (70.3 kB)
avatar image JPhilipp · Mar 11, 2020 at 08:05 PM 0
Share

@dirilikerhan355 Consider using Vector3.Lerp between your start and end point; the third parameter, t, defines the interpolant distance between 0 (start) and 1 (end), so would be scaled appropriately.

2 Replies

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

Answer by dirilikerhan355 · Mar 12, 2020 at 04:05 PM

thank you @unity_ek98vnTRplGj8Q this code is works!!!

public float distanceBetweenNodes;

Update() { transform.position = Vector3.Lerp(node1.transform.positon, node2.transform.position, distanceBetweenNodes); distanceBetweenNodes = Vector3.Distance(transform.positon, node1) / Vector3.Distance(node1, node2); }

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
1

Answer by unity_ek98vnTRplGj8Q · Mar 11, 2020 at 08:09 PM

I would start by getting the direction between the two points:

 Vector3 direction = node2.transform.position - node1.transform.position;
 direction.Normalize();

Now you can move along the line by adding the direction vector to the starting node position

 //Move 1 unit along the line
 transform.position = node1.transform.position + direction;

You can multiply the direction by whatever distance you want to move along the line.

 //Move 5 units along the line
 transform.position = node1.transform.position + 5*direction;

If you want to move a certain percentage of the length of the line, Vector3.Lerp is a good shortcut

 //Move 30% of the way down the line
 transform.position = Vector3.Lerp(node1.transform.position, node2.transform.position, 0.3f);

If you want to move along the line smoothly, use one of the above methods but just do a tiny amount each frame

 void Update(){
     transform.position = transform.positon + direction * speed * Time.deltaTime;
 }
Comment
Add comment · Show 1 · 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 unity_ek98vnTRplGj8Q · Mar 11, 2020 at 08:13 PM 0
Share

Re-reading you question I think maybe you want the object to maintain its position along the line even while you move the points? If this is the case, Vector3.Lerp is def the way to go, just keep track of how far you were between the nodes

 public float distanceBetweenNodes;
 
 Update(){
     transform.position = Vector3.Lerp(node1.transform.positon, node2.transform.position, distanceBetweenNodes);
     distanceBetweenNodes = Vector3.Distance(transform.positon, node1) / Vector3.Distance(node1, node2);

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

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

124 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

Related Questions

Find two points which are on 120 degrees from a third point. 3 Answers

Clamp rotation of sprite? 0 Answers

How to have Multiple Camera Positions with SmoothFollow.js ? 1 Answer

Setting rotation Angle / rotation question 1 Answer

Rotation around a moving object. 0 Answers

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