【大概是最簡(jiǎn)單的unity教程】UP: Yu_Zhen
1、如何理解?
float x, z;
?x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
https://blog.csdn.net/weixin_44869410/article/details/103210842
在 void Update() 每一幀都會(huì)刷新,所以只有1,-1,0 三種情況。
鍵盤(pán)WASD,為例
W Z軸的前進(jìn) 1
S? Z軸的前后退 -1?
A X軸前進(jìn) 1 D同同理
2、
Vector3 mov;
mov = transform.right * x + transform.forward * z;
plyerMove.Move(mov*speed*Time.deltaTime);
?transform.right X軸的要移動(dòng)的方向?
?transform.forward Z軸的要移動(dòng)的方向?
*x *z 代表這個(gè)方向移動(dòng)的距離數(shù)值的大小
move 用來(lái)裝這個(gè)數(shù)據(jù)的一個(gè)結(jié)構(gòu)體
Move 移動(dòng)函數(shù)
3、cameraLook
a、鼠標(biāo)左右,人物移動(dòng) X軸
b、上下移動(dòng) Y軸 鏡頭移動(dòng)
public Transform player;
float x, y;
x = Input.GetAxis("Mouse X") * mouseSpeed*Time.deltaTime; //獲取鼠標(biāo)X軸數(shù)據(jù)
y = Input.GetAxis("Mouse Y") * mouseSpeed*Time.deltaTime;
player.Rotate(Vector3.up * x); // player 在unity掛載對(duì)象應(yīng)該是player這個(gè)對(duì)象
// 這里的this 是camera?
this.transform.localRotation = Quaternion.Euler(ymove,0,0);
P3
? ? //?
? ? public float speed;
? ? // 按一下空格 跳躍的高度 而不是速度
? ? public float jumpSpeed = 10;
? ? public float g = 10;
? ? public CharacterController plyerMove;
? ? public int jumpCount = 10;
? ? Vector3 move;
? ? // 二段跳
? ? // private int jumpOnAir = 0;
? ? // Start is called before the first frame update
? ? void Start()
? ? {
? ? ? ??
? ? }
? ? // Update is called once per frame
? ? void Update()
? ? {
? ? ? ? float x = 0, z = 0;
? ? ? ? if (plyerMove.isGrounded) {
? ? ? ? ? ? x = Input.GetAxis("Horizontal");
? ? ? ? ? ? z = Input.GetAxis("Vertical");
? ? ? ? ? ? move = (transform.right * x + transform.forward * z)*speed;
? ? ? ? ? ?jumpCount = 10;
? ? ? ? }
? ? ? ? if (isPressJump() && jumpCount >0)
? ? ? ? {
? ? ? ? ? ? move.y = jumpSpeed;
? ? ? ? ? ? // --的條件要改一下 限制 不能2幀全沒(méi)了
? ? ? ? ? ? jumpCount--;
? ? ? ? }
? ? ? ? // 為什么會(huì)掉落 應(yīng)該和這個(gè)計(jì)算方式有關(guān)
? ? ? ? // 為什么沒(méi)有這句就跳不起來(lái) 這是計(jì)算公式 一定要這樣帶
? ? ? ? move.y = move.y - g * Time.deltaTime;
? ? ? ? // move 向量 代表方向
? ? ? ? plyerMove.Move( move * Time.deltaTime );
? ? }
? ? bool isPressJump()?
? ? {? ?
? ? ? ? /*
? ? ? ? if (Input.GetAxis("Jump") == 1){
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? else {
? ? ? ? ? ? return false;
? ? ? ? }*/
? ? ? ? return Input.GetAxis("Jump") == 1 ? true : false;
? ? }