unity學習筆記(持續更新)
1.點哪指向哪
if (Input.GetMouseButtonDown(0))
{
//相機向指定的物體發出射線,并回傳
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//射線可以跟collider組件的物體發生碰撞
RaycastHit hit;//碰撞的資訊
//表示是否碰到東西
if (Physics.Raycast(ray, out hit))
{
transform.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z));
}
}
2.物體的旋轉
#region 方法一
//設定炮臺方向跟小球一個方向
//transform.forward = ball.position - transform.position;
#endregion
#region 方法二
// transform.LookAt(ball);
#endregion
//Vector3.Angle得到的夾角只會回傳正夾角
#region 方法二 改旋轉
//改旋轉軸
// Vector3 ballPos = ball.position - transform.position;
// float angle = Vector3.Angle(ballPos,dir);
Vector3 axis = Vector3.Cross(dir, ballPos);
// dir = ballPos;
// //dir = new Vector3(ballPos.x, 0, ballPos.z);
// transform.Rotate(Vector3.up, angle);
// transform.Rotate(axis, angle);
//改旋轉角度
Vector3 ballPos = ball.position - transform.position;
float angle = Vector3.Angle(ballPos,dir);
Vector3 axis = Vector3.Cross(dir, ballPos);
float result = Vector3.Dot(axis, Vector3.up);
//根據傳入值的符號回傳,如果傳入的值為負,則回傳-1,否則回傳1
result = Mathf.Sign(result);
dir = ballPos;
angle *= result;
transform.Rotate(Vector3.up, angle);
3.鍵盤事件
鍵盤輸入
Input.GetKey長安一個鍵不放,每幀判斷一次
Input.GetKeyDown按下某個鍵,只判斷一次
Input.GetKeyUp松開某個鍵,只判斷一次
if (Input.GetKeyUp(KeyCode.W))
{
Debug.Log("往前走");
}
if (Input.GetKeyDown(KeyCode.A))
{
transform.Translate(new Vector3(-speed, 0, 0));
Debug.Log("往左走");
}
if (Input.GetKeyDown(KeyCode.D))
{
transform.Translate(new Vector3(speed, 0, 0));
Debug.Log("往右走");
}
if (Input.GetKeyDown(KeyCode.S))
{
transform.Translate(new Vector3(0, 0, -speed));
Debug.Log("往下走");
}
4.滑鼠事件
滑鼠輸入
Input.GetMouseButton按住不放 每幀判斷
Input.GetMouseButtonDown點擊時 判斷一次
Input.GetMouseButtonUp松開時 判斷一次
0代表滑鼠左鍵,1代表右鍵,2代表中鍵
if (Input.GetMouseButtonDown(0))
{
print("點擊左鍵");
}
if (Input.GetMouseButtonDown(0))
{
print("點擊右鍵");
}
if (Input.GetMouseButtonDown(0))
{
print("點擊中鍵");
}
5.通過GUI代碼改ui
//OnGUI為每幀執行
private void OnGUI()
{
GUIStyle style = new GUIStyle();
style.fontSize = 20;
style.normal.textColor = Color.blue;
GUI.Label(new Rect(0, 0, 500, 300), "得分:"+ sum,style);
}
private void OnGUI()
{
if( GUI.Button(new Rect(Screen.width / 2 - width / 2, Screen.height / 2 - height / 2, width, height), "開始游戲"))
{
SceneManager.LoadScene(1);
}
}
6.場景的切換
1.引入命名空間
using UnityEngine.SceneManagement;
2.場景跳轉
SceneManager.LoadScene("SampleScene");
7.物理碰撞
private void OnCollisionEnter(Collision collision){}
8.普通的碰撞
private void OnTriggerEnter(Collider other){}
9.公轉
transform.RotateAround(target.position, Vector3.up, angle * Time.deltaTime);
10.物體跟隨滑鼠轉
Vector3 mousePos;
public float rate;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
mousePos = Input.mousePosition;
}
if (Input.GetMouseButton(0))
{
//滑鼠移動的偏移量
Vector3 offest = Input.mousePosition - mousePos;
//旋轉物體
transform.eulerAngles += Vector3.down * offest.x * rate;
mousePos = Input.mousePosition;
}
}
11.獲取子節點在父節點下的坐標
字符節點的操作都是在transfrom組件上完成的
//通過下標獲取子節點
transform.GetChild(index)
GameObject.Find("needles").transform.GetChild(0).position = new Vector3(0, 0.7f, -5);
//獲取子節點在父節點下的下標
transform.GetSiblingIndex();
//子節點的個數
transform.childCount
12.給物體給力讓其運動
if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody>().AddForce(Vector3.left * v);
}
if (Input.GetKey(KeyCode.S))
{
GetComponent<Rigidbody>().AddForce(Vector3.back * v);
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody>().AddForce(Vector3.right * v);
}
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * v * 30);
}
13.計時器
Invoke("spinDrop", 1.0f);
14.世界坐標轉本地坐標
Vector3 dir = new Vector3(horizontal, 0, vertical);
//將世界坐標轉換成本地坐標
dir = transform.TransformDirection(dir);
15.控制一個值的最大值和最小值,即范圍
xAngle = Mathf.Clamp(xAngle, yMin, yMax);
16.代碼控制影片
Play("ation 1" );,播放影片,傳入引數為影片名字
Stop("ation 1") ,停止影片,傳入引數為影片名字
CrossFade("ation 1", 0.5f); ,有過度的切換影片,傳入引數(影片名字,過度時間)
17.隱藏滑鼠
//隱藏鎖定滑鼠
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
18.節點的銷毀
Destroy(this.gameObject,1); //1秒后銷毀
19.查找節點
//從當前場景中所有的物體里尋找叫這個名字的物體 回傳值是GameObject型別
// 找到第一個同名的節點,費性能,概率性找不到,可能會有多個重名的物體
GameObject.Find()
//從transfrom組件的子物體中尋找叫這個名字的物體,找不到孫子節點 回傳值是Transfrom型別
transfrom.Find()
20.Gizmos標記
//和update類似,也是每幀呼叫一次
//OnDrawGizmos是在代碼編譯完成時開始執行
private void OnDrawGizmos()
{
//Gizmos.DrawCube(Vector3.zero, Vector3.one);
Gizmos.color = Color.red;
for (int i = 0; i < points.Length - 1; i++)
{
Gizmos.DrawLine(points[i].position, points[i + 1].position);
}
}
21.資料本地保存
PlayerPrefs.SetInt("num",num);
PlayerPrefs.GetInt("num",0);
22.代碼系結按鈕點擊事件
//只能是無參無回傳值的函式
GetComponent<Button>().onClick.AddListener(OnClick);
25.高亮的實作
https://www.jianshu.com/p/23fb2f6fcfa0
26.防止射線點擊ui的時候穿透
EventSystem.current.IsPointerOverGameObject() 如果當前滑鼠在 ui 上回傳true 否則回傳false
27.關于腳本組件屬性的注釋
[Header("兩次巡邏的間隔時間")] //注釋
public float patrol_waite_time = 3;//兩次巡邏的間隔時間
[SerializeField] //在面板顯示私有的屬性
float timer;//實作計時器的引數
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/311142.html
標籤:其他
