一、 Input输入类
- GetKeyxxx:键盘上的按键
Getkey:按下后每帧启动
Getkeydown:按下启动一次
Getkeyup:放开启动一次
void Update () {
if(Input.GetKeyDown(KeyCode.Space))
{
print("KeyDown");
}
if (Input.GetKeyUp(KeyCode.Space))
{
print("KeyUP");
}
}
Key name:
2. GetMouseButton:一直监听,
GetMouseButtonDown:监听一次鼠标按下
GetMouseButtonUp:监听一次鼠标放开
3. GetButtonxxx——同GetMouseButton原理相同,但是得自己设置虚拟按键
虚拟按键打开方式
4. GetAxis——控制轴的按下,可以实现控制物体的运动
cube.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal"));
GetAxisRaw——按下可以迅速反应,没有延迟
- input.mouseposition——可以输出鼠标在屏幕上的坐标
二、 vector2
- down(0,-1)
up(0,1)
right(1,0)
left(-1,0)
one(1,1)
zero(0,0)
normalized:单位化向量
magnitude:向量的长度
sqrmagnitude:开平方向量的长度 - vector2/3是结构体/向量是结构体,是值类型,要整体赋值
transform.position = new Vector3(3, 3, 3);
Vector3 pos = transform.position;
pos.x = 10;
transform.position = pos;
结构体单独修改向量值和整体修改向量值
3. 静态方法
Angle:向量夹角度数
Clampmagnitude:限定长度
Distance:两点之间的距离
Lerp:两点差值
LerpUnclamped:两点差值,超出去继续向外延伸
Movetowards(a,b,x):a——b每x秒的匀速运动
4. vector3
down(0,0,-1)
up(0,,0,1)
right(0,1,0)
left,0,-1,0)
one(1,1,1)
zero(0,0,0)
project:一个向量在OnNormal上的投影
Slerp(向量a,向量b,t):按夹角a与b的差值
5. vector2/3的加减乘除
Vector2 a = new Vector2(2, 2);
Vector2 b = new Vector2(3, 4);
Vector2 c = new Vector2(3, 0);
Vector2 res = b - a;//1,2
print(res);
print(res * 10);
print(res / 5);
print(a + b);
print(a = b);
三、 Random随机数
- Range(a,b):随机生成,但不会生成最大值b
print(Random.Range(4, 10));
print(Random.Range(4, 5f));
- InitState(int seed):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AOI01 : MonoBehaviour {
// Use this for initialization
void Start () {
//Random.InitState((int)System.DateTime.Now.Ticks);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
print(Random.Range(4, 100));
print((int)System.DateTime.Now.Ticks);
}
}
}
如果生成相同的随机数的话,可以改为
Random.InitState(0);//每次打开的随机数相同
3. insideUnitCircle:在圆周围随机生成
insideUnitSphere:在球体之间随机生成
cube.position = Random.insideUnitCircle * 5;
cube.position = Random.insideUnitSphere * 5;
用于敌人的随机生成
四、 Quatermion四元数,欧拉角
表示欧拉角
欧拉角与四元数的区别:
print(cube.eulerAngles);
print(cube.rotation);
欧拉角与四元素的转换:
欧拉角转四元素
1.
cube.eulerAngles = new Vector3(10, 0, 0);
2
cube.rotation = Quaternion.Euler(new Vector3(45, 45, 45));
四元素转欧拉角:
print(cube.rotation.eulerAngles);
把前方向转换为四元素
LookRotation:
if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 dir = enemy.position - player.position;
dir.y = 0;
player.rotation = Quaternion.LookRotation(dir);
}
}
Slerp比lerp比较平缓
Vector3 dir = enemy.position - player.position;
dir.y = 0;
Quaternion target = Quaternion.LookRotation(dir);
player.rotation = Quaternion.Slerp(player.rotation,target,Time.deltaTime);
五、 Rigibody刚体组件
- 刚体组件修改position和rotation
Position:
// playerRgd.position = playerRgd.transform.position + Vector3.forward * Time.deltaTime;
playerRgd.MovePosition(playerRgd.transform.position + Vector3.forward * Time.deltaTime);
持续运动用MovePosition
Rotation:
2. AddForce:施加力的方式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AOI01 : MonoBehaviour {
public GameObject playerRgd;
public int force;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate() {
GetComponent<Rigidbody>().AddForce(Vector3.forward * force);
}
}
六、 camera
- 把鼠标点转换成射线
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AOI01 : MonoBehaviour {
public Camera maincamera;
// Use this for initialization
void Start () {
//maincamera = GameObject.Find("MainCamera").GetComponent<Camera>();
maincamera = Camera.main;
}
// Update is called once per frame
void Update() {
Ray ray = maincamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollinder = Physics.Raycast(ray, out hit);
if (isCollinder)
{
Debug.Log(hit.collider);
}
}
}
- field od view:摄像机的角度大小
- Clipping Planes:设置最近和最远距离
- Viewport Rect:设置窗口大小
七、 通过Application获取datapath
Datapath:数据的路径
Streaming Assets文件夹可以独自打包
StreamingAssetspath:文件浏览读取数据
persistenDataPath:实例化数据
temporaryCachePath:临时的缓冲数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AOI01 : MonoBehaviour {
// Use this for initialization
void Start () {
print(Application.dataPath);
print(Application.streamingAssetsPath);
print(Application.persistentDataPath);
print(Application.temporaryCachePath);
}
// Update is called once per frame
void Update() {
}
}