This site uses strictly necessary cookies. More Information

X- Home /

# How to rotate objects around point while always crossing a point?

So I am Instantiating some gameObjects (green cubes) along along a circle outline. Now I want to rotate them around the yellow triangle in the center, however I want them to always cross the red cross over the triangle. What would be the easiest way to achieve this? I have some approaches in my mind however they seem to be overcomplicated.I also tried to use the rotateAround function, however this way I wasn't able to make the objects cross one another, since I can only rotate over one axis.

**Answer** by Namey5
·
Aug 22, 2020 at 12:42 PM

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);
```

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.

**Answer** by Ymrasu
·
Aug 22, 2020 at 12:38 PM

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).

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.