# The operation of rotating around the axis of unity

Time：2022-5-21

``````using UnityEngine;
using System.Collections;
public class TestRotateRound : MonoBehaviour
{
public GameObject Sphere;
private float curtTime = 0.0f;
void Update()
{
//Use c# encapsulated code rotatearound
gameObject.transform.RotateAround(Sphere.transform.position, Sphere.transform.up, 72 * Time.deltaTime);
//Encapsulate the code by yourself, and the function is the same as that above
//RotateAround(Sphere.transform.position,Vector3.up, 72 * Time.deltaTime);
}
private void RotateAround(Vector3 center, Vector3 axis, float angle)
{
Quaternion rotation = Quaternion.AngleAxis(angle, axis);
//Before rotating, take center as the starting point and transform Position the vector whose current object position is the end point
Vector3 beforeVector = transform.position - center;
//Quaternion * vector (the position cannot be changed, otherwise a compilation error occurs)
Vector3 afterVector = rotation * beforeVector;// Vector after rotation
//End of vector = start of vector + vector
transform.position = afterVector + center;
//Look at the sphere so that the Z axis points to the sphere
transform.LookAt(Sphere.transform.position);
}
}``````

Supplement: unity is a trap that rotates around the X axis and limits the angle

When making an FPS camera, we encounter the need to limit the angle. The angle of view can only be viewed in the range of – 60 to 60 degrees. In the transform component of unity, rotate counterclockwise around the X axis, and the localeuler angle of the transform component will increase in the range of 0 ~ 360 (as shown in the figure)

## The key lies in the angle conversion, which is directly on the code

``````
public static void RotateClampX(this Transform t, float degree, float min, float max)
{
degree = (t.localEulerAngles.x - degree);
if (degree > 180f)
{
degree -= 360f;
}
degree = Mathf.Clamp(degree, min, max);
t.localEulerAngles = t.localEulerAngles.SetX(degree);
}
``````

Supplement: unity3d realizes that an object always faces another object (rotating around an axis and quaternion)

At first, I was obsessed with how to use the handle to control the rotation of the object in VR, and the position of the object remained unchanged.

Equivalent to: globe. More generally speaking, a stick is inserted into the globe, and then holding the stick can control the rotation of the globe. The handle is equivalent to that stick.

## The code is as follows:

``````
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);``````

This code realizes that mytransform can always rotate according to the target, and rotationspeed controls the speed.

Of course, the above sentence is only always facing, and has not added the initial rotation of the target recorded at the beginning. Otherwise, you should follow the handle at the beginning, rather than control yourself at will. For the understanding of the above sentence, I will post it after I understand it.

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If there are mistakes or not fully considered, please don’t hesitate to comment.

## Highly recommended – Ajax Technology Resource Center

AJAX Technology Resource Center Web applications make it possible to interact across the Internet, but have your users ever complained about the pain of waiting for web pages to refresh again and again? Now, it’s time to regroup your skills and create web applications with rich interactivity based on Ajax technology! Ajax (asynchronous JavaScript and […]