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 Lafikobra · Dec 20, 2019 at 05:01 PM · rotationmouse positionrotation detection

Unity 2d object advanced rotation

![alt text][1] [1]: /storage/temp/150281-graf.png

Hello there :D Can someone help me with his? Rotating an object compared to where you grab it with the mouse. So if i grab for example a rectangle on its left side, it should rotate by its right side on the z-axis by the mouse position, and vica versa. if i grab it in the right side it sould turn by its left side. I hope my little graph is more clear :D I would very very very appreciate your help

graf.png (83.2 kB)
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
0

Answer by unity_ek98vnTRplGj8Q · Dec 20, 2019 at 10:40 PM

Your question looks extremely similar to this one: https://answers.unity.com/questions/1685799/how-to-rotate-a-line-by-its-other-end.html


I've taken that code and modified it to actually rotate a sprite instead of just drawing a line with a lineRenderer. You can take out the line renderer code if you want, but it will make it easy for you to see what's going on. Don't forget to set spriteLength to the actual length of your sprite. Most of the changes I made I just tacked on to the end of SetLinePoints()

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DisRotTest : MonoBehaviour
 {
 
     public Transform point1, point2;
     public Transform sprite; 
     
     public Camera cam;
     public float spriteLength = 10;
 
     private LineRenderer lr;
     private Transform grabbedPoint, stationaryPoint;
     private Vector3[] lrPoints;
     private Vector3 offset;
     void Start()
     {
         //Set the initial line length to 10
         point2.position = point1.position + spriteLength * Vector3.right;
 
         //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))
         {
             offset = mousePos - stationaryPoint.position;
             offset = offset.normalized * spriteLength; //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);
 
         //Move sprite to the right of point1 and reset its rotation
         sprite.transform.position = point1.position + new Vector3(spriteLength/2f, 0,0);
         sprite.rotation = Quaternion.identity;
 
         //May have to flip some things based on rotation and which point we grabbed
         float isNegative = offset.y < 0 ? -1 : 1;
         float negativeOffset = stationaryPoint == point1 ? 1 : -1;
 
         //Rotate sprite AROUND point1 by the vector angle between point 1 and point 2
         sprite.RotateAround(point1.position, Vector3.forward, negativeOffset*isNegative*Vector3.Angle(Vector3.right, negativeOffset*offset));
     }
 }
 

Let me know if this works for you

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 Lafikobra · Dec 21, 2019 at 02:56 PM 0
Share

Hi there @unity_ek98vnTRplGj8Q :D First of all, thanks for Your reply. Actually, that other code You linked is could be a better sollution. $$anonymous$$y goal here is to draw straight lines with mouse (mouse button click startpoint, mouse button up endpoint of the line) with a collider on them, and than edit while dragging on its ends.I attached a gif from a game to make it more understandable.If i may ask you, can You modify that code one last time? :S alt text

line.gif (422.7 kB)
avatar image unity_ek98vnTRplGj8Q Lafikobra · Dec 23, 2019 at 04:03 PM 0
Share

I recommend giving this a shot yourself. What you will need to do: make a prefab that has a linerenderer, boxCollider2D, and 2 points as well as a script that you can use to get the references to all these things (just a script with some public members that you can access from your main script). You will need a way to keep track of all of these lines, so I recommend that in the Start() of this script you add that instance to a static list so that you can access them all later.


Now, all you have to do is when you click, loop through all points and grab them if they are within a certain distance. If none are, you can Instantiate a new instance of the line prefab and grab its points. The rest of the script is going to be pretty much the same, just make sure you scale the boxCollider to how you need it (just set BoxCollider2D.size to the width and height of the line).


If you need more help I recommend opening a new question or contacting me directly, as this is starting to get out of the scope of your original question. Let me know if you want my email

avatar image Lafikobra unity_ek98vnTRplGj8Q · Dec 23, 2019 at 05:05 PM 0
Share

Hi there @unity_ek98vnTRplGj8Q :D Well, i almost got it working by now, but somehow when i grab one of its end points its just refuses to update the line.. Yes, it would be much more easy trough mail, so yeah send me your mail (or discord if you have)and i show you what ive got so far :D I think its just needs some refining.

Show more comments

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

153 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

Related Questions

Why does the child of a game object not rotate to face mouse position when the parent object is moving? 1 Answer

Help rotating gun towards mouse position (with flipping) 1 Answer

Align a vector3 with parent's up-vector. 2 Answers

Making Child Object's Rotation Relative to Parent's Movement Direction,Making Child's Rotation Match Parent's Movement Direction? 0 Answers

A small problem with rotating object at mouse position 1 Answer


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