- Home /
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
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
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
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
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.