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 /
avatar image
0
Question by Geri860 · Dec 19, 2019 at 07:53 PM · rotationlinerenderer

How to rotate a line by its other end at runtime?

So basically i want to rotate a line by its other end. More clearly, i draw one straight line which is 10x1 long. I want to grab one of its end with the mouse and than it would rotate around its other ends positions. Is this possible?

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 unity_ek98vnTRplGj8Q · Dec 19, 2019 at 10:35 PM

Here is a short script I made that accomplishes what I think you are asking for. Keep track of the ends of the line with two points and just use Camera.ScreenToWorldPoint(Input.mousePosition) to rotate the points. If this doesn't do what you want it should at least give you an idea of what you need to do.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DisRotTest : MonoBehaviour
 {
 
     public Transform point1, point2;
     public Camera cam;
 
     private LineRenderer lr;
     private Transform grabbedPoint, stationaryPoint;
     private Vector3[] lrPoints;
     void Start()
     {
         //Set the initial line length to 10
         point2.position = point1.position + 10 * Vector3.up;
 
         //I'm using a line renderer to draw the line
         lr = GetComponent<LineRenderer>();
         lrPoints = new Vector3[2];
 
         //Tell the line renderer to update its points
         SetLinePoints();
 
         grabbedPoint = stationaryPoint = null;
     }
 
     private void Update()
     {
         //Grab mouse position every frame
         Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
 
         //I want to rotate on the X-Y plane, so I'm going to align the mousePosition with my gameObject on the Z axis
         mousePos.z = transform.position.z;
 
         //When we click, grab the closest point
         if (Input.GetMouseButtonDown(0))
         {
             grabbedPoint = Vector3.Distance(mousePos, point1.position) < Vector3.Distance(mousePos, point2.position) ? point1 : point2;
             stationaryPoint = grabbedPoint == point1 ? point2 : point1;
         }
 
         //While we drag, make the line go through our mouse position
         if (Input.GetMouseButton(0))
         {
             Vector3 offset = mousePos - stationaryPoint.position;
             offset = offset.normalized * 10; //Set line length to 10
             grabbedPoint.position = stationaryPoint.position + offset;
         }
 
         //Tell the line renderer to update
         SetLinePoints();
     }
 
     private void SetLinePoints()
     {
         lrPoints[0] = point1.position;
         lrPoints[1] = point2.position;
         lr.SetPositions(lrPoints);
     }
 }
 
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 Geri860 · Dec 20, 2019 at 04:13 PM 0
Share

Heyy :D Thank you very very much!!!!

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

152 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

Related Questions

Setting rotation of player equal to rotation of a Line from Line Renderer,Get the rotation of Line Renderer and equal it to the rotation of the player 0 Answers

Line Renderer Rotation Issue 2 Answers

Line renderer rotation and alignment 1 Answer

About Tile Texture for 2D Line Renderer 0 Answers

slowly rotate a object *need quick fix* 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