Summary of common methods in Unity (tips)

get directions

//获取从A点到B点的方向向量
var direction = B.transform.position-A.transform.position;
direction.Normalize();//根据需求是否进行向量化

point an object in a certain direction

var direction = B.transform.position-A.transform.position;//获取方向
transform.rotation=Quaternion.Euler(Mathf.Atan(direction.y,direction.z)*Mathf.Rad2Deg));//根据方向计算偏移量进而旋转朝向某物体

Mouse hides and pins to center

Cursor.visible = false;//控制隐藏
Cursor.lockState = CursorLockMode.Locked;//控制是否锁定鼠标

Draw lines in the Unity window

 void OnDrawGizmosSelected()//一定要在这个函数下才能实现画线
{
    
    
        // Left/right and up/down axes.
        Gizmos.color = Color.white;
        Gizmos.DrawLine(transform.position - new Vector3(2.25f, 0, 0), transform.position + new Vector3(2.25f, 0, 0));
}

Switch InputSystem time mode

InputSystem.settings.updateMode = InputSettings.UpdateMode.ProcessEventsInDynamicUpdate;//无视物理时间
InputSystem.settings.updateMode = InputSettings.UpdateMode.ProcessEventsInFixedUpdate;//使用物理时间,受TimeScale影响

Play multiple sound effects

audioSource.PlayOneShot(片段,音量);//防止播放器短时间播放多条音效导致上条音效未播放完就切换到下条

X-ray inspection

Physics.Raycast(origin(V3), direction(V3), hitInfo(RaycastHit), distance(float), LayerMask(int));//初始点,方向,碰撞信息,距离(可省略不写),碰撞图层(默认全选)

2D character card wall

1. Create a new Physical material 2D first, and you may encounter a problem here: the official prompt Assets > Create > Physics Material 2D
2. Change the friction to 0
insert image description here
3. Assign the material to the player
insert image description here

Guess you like

Origin blog.csdn.net/m0_48554728/article/details/130081561