- Home /
trying to RotateAround an object around another object using mouse
This is almost working. I just can't figure convert the angle given from rotZ in the RotateAround. What it seems to be going wrong is that it's not using rotZ as an angle to rotate to, and instead takes it as a speed to rotate at. How do I make it rotate to that number as an angle instead of taking it as a number to increase its angle by?
private Vector3 zAxis = new Vector3(0, 0, 1);
public Camera cam;
void Update()
{
Vector3 difference = cam.ScreenToWorldPoint(Input.mousePosition) - transform.parent.transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.RotateAround
(
this.transform.parent.transform.position,
zAxis,
rotZ
);
}
Answer by Leafenzo · Feb 09, 2019 at 07:41 PM
I found a workaround that may only really work for my exact situation, I take a couple specific angles then place the transform I'm trying to move a distance from the center along those angles.
I did receive help for a majority of this code from another user on this forum.
Original code just for drawing some gizmo lines on angles:
private void OnDrawGizmos()
{
// / Special thanks Ymrasu
Gizmos.color = Color.yellow;
float length = 10f;
var startPoint = transform.position;
float angle1 = ((rotZone * 45) + 90 + 22.5f) * Mathf.Deg2Rad;
float angle2 = ((rotZone * 45) + 90 - 22.5f) * Mathf.Deg2Rad;
var endPoint1 = new Vector2(startPoint.x + length * Mathf.Cos(angle1), startPoint.y + length * Mathf.Sin(angle1));
var endPoint2 = new Vector2(startPoint.x + length * Mathf.Cos(angle2), startPoint.y + length * Mathf.Sin(angle2));
Gizmos.DrawLine(startPoint, endPoint1);
Gizmos.DrawLine(startPoint, endPoint2);
}
Reused to place a transform on the end of a line
public Transform atkPos;
public float distance = 1f;
public float Rotoffset;
private void Update ()
{
///desRotA = transform.rotation.z;
///atkPos.RotateAround(this.transform.position, zAxis, Mathf.Abs(90 - desRotA));
float angle = ((rotZone * 45) + 90) * Mathf.Deg2Rad;
var startPoint = transform.position;
var endPoint = new Vector2(startPoint.x + distance * Mathf.Cos(angle), startPoint.y + distance * Mathf.Sin(angle));
atkPos.position = endPoint;
Vector3 difference = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + Rotoffset);
findRotationzone();
}
I'm not marking this as the answer though, as this only works for 8-degree rotation (Although It wouldn't be too hard to make it work for 360.), as it doesn't use RotateAround, and as it does not rotate the moved transform with it's rotation around an object. But this might still be helpfull sometime though.
Sorry if I misunderstood your question, but you're not just trying to get your object to rotate towards the mouse, are you?
Sorry If I phrased it confusedly. I am not trying to get my object to rotate towards the mouse, I'm trying to rotate it around another object to face the mouse compared to the other object. Using the RotateAround function, as in it rotates around another object.
Ah, gotcha :) Well I'm happy you found a solution :)
Your answer
Follow this Question
Related Questions
Proper way to rotate an object? 1 Answer
RotateAround reversing doesn't work with negative vector3 0 Answers
Rotation Problem - Simulating Gimbal lock 1 Answer
Rotation promlems past 90 and 270 3 Answers
Rotation not observing limits 1 Answer