Comments and answers for "How to rotate objects around point while always crossing a point?"
http://answers.unity.com/questions/1764440/how-to-rotate-objects-around-point-while-always-cr.html
The latest comments and answers for the question "How to rotate objects around point while always crossing a point?"Comment by Filip8429 on Filip8429's answer
http://answers.unity.com/comments/1764683/view.html
Nice, this worked perfectly and was so simple, thank you very much! I have one thing that I am still trying to figure out. I used the radius of a Sphere Collider to get the height of the crossing point, however I am trying to randomize this point a bit, so that the green cubes cross somewhere on top of the Sphere Do you know if Unity has a function for this. Like crossing a cylinder with the Sphere and then using a random Position on the intersectioning part.Sun, 23 Aug 2020 06:25:13 GMTFilip8429Answer by Namey5
http://answers.unity.com/answers/1764451/view.html
Assuming the green points are known initial positions defining where the rotations should begin, you can use the cross product to find an axis to rotate around relative to the cross;
//Assuming;
// - this.transform = yellow triangle
// - object = green square
// - targetPoint = red cross
//Find the vector from the triangle to the cross
Vector3 v0 = targetPoint.position - transform.position;
//Find the vector from the triangle to the square
Vector3 v1 = object.position - transform.position
//Find the vector perpendicular to both of the above vectors
Vector3 axis = Vector3.Cross (v0, v1).normalized;
//Rotate the square around the triangle at 5 degrees/second, passing through the cross
object.RotateAround (transform.position, axis, 5f * Time.deltaTime);Sat, 22 Aug 2020 12:42:26 GMTNamey5Answer by Ymrasu
http://answers.unity.com/answers/1764450/view.html
This topic seems to have been coming up a few recently. I have a method that rotates one point around another. Here is what I came up with for your situation:
using UnityEngine;
public class RotateAround : MonoBehaviour
{
public Transform centerObject; // Assign in editor or whatever
public float distance; // units from center
public float angle; // in degrees
public bool xAxis; // around x axis?
public float rotationSpeed; // degrees per second
private void Update()
{
// set target distance (Vector3.up is arbitrary, we just need it for the offset)
Vector3 targetPosition = centerObject.position + (Vector3.up * distance);
// set target angle around orbit
// set angle by time to continuously rotate
angle = rotationSpeed * Time.time;
// this step is not necessary, but lets keep angle within 360 degrees
angle %= 360;
if (xAxis) {
targetPosition = RotatePointAroundPointXAxis(targetPosition, centerObject.position, angle);
} else {
targetPosition = RotatePointAroundPointZAxis(targetPosition, centerObject.position, angle);
}
// move to targetPosition
transform.position = targetPosition;
}
Vector3 RotatePointAroundPointZAxis(Vector3 point1, Vector3 point2, float angle)
{
angle *= Mathf.Deg2Rad;
var x = Mathf.Cos(angle) * (point1.x - point2.x) - Mathf.Sin(angle) * (point1.y - point2.y) + point2.x;
var y = Mathf.Sin(angle) * (point1.x - point2.x) + Mathf.Cos(angle) * (point1.y - point2.y) + point2.y;
return new Vector3(x, y, point2.z);
}
Vector3 RotatePointAroundPointXAxis(Vector3 point1, Vector3 point2, float angle)
{
angle *= Mathf.Deg2Rad;
var y = Mathf.Cos(angle) * (point1.y - point2.y) - Mathf.Sin(angle) * (point1.z - point2.z) + point2.y;
var z = Mathf.Sin(angle) * (point1.y - point2.y) + Mathf.Cos(angle) * (point1.z - point2.z) + point2.z;
return new Vector3(point2.x, y, z);
}
}
The two methods are the same, but with the axis switched around.
Just attach the script to your two green objects, reference your center object and set the bool for one of them.
The two objects will cross at your X spot whenever the angle is 0 for both. (You can set the rotationSpeed differently for both and they'll still cross at X at some time (ie, 45 rotation speed and 90 rotation speed).Sat, 22 Aug 2020 12:38:37 GMTYmrasu