【大概是最简单的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() 每一帧都会刷新,所以只有1,-1,0 三种情况。
键盘WASD,为例
W Z轴的前进 1
S Z轴的前后退 -1
A X轴前进 1 D同同理
2、
Vector3 mov;
mov = transform.right * x + transform.forward * z;
plyerMove.Move(mov*speed*Time.deltaTime);
transform.right X轴的要移动的方向
transform.forward Z轴的要移动的方向
*x *z 代表这个方向移动的距离数值的大小
move 用来装这个数据的一个结构体
Move 移动函数
3、cameraLook
a、鼠标左右,人物移动 X轴
b、上下移动 Y轴 镜头移动
public Transform player;
float x, y;
x = Input.GetAxis("Mouse X") * mouseSpeed*Time.deltaTime; //获取鼠标X轴数据
y = Input.GetAxis("Mouse Y") * mouseSpeed*Time.deltaTime;
player.Rotate(Vector3.up * x); // player 在unity挂载对象应该是player这个对象
// 这里的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帧全没了
jumpCount--;
}
// 为什么会掉落 应该和这个计算方式有关
// 为什么没有这句就跳不起来 这是计算公式 一定要这样带
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;
}