using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
//获得CharacterController组件
CharacterController characterController;
//水平移动速度
public float walkSpeed = 5;
//跳跃高度
public float jumpHeight = 5;
//重力
public float gravity = 10;
//角色移动方向
Vector3 moveDirection = Vector3.zero;
float horizontal = 0;
// Use this for initialization
void Start () {
//获取Player中的CharacterController组件
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//角色向着所指示的方向移动
characterController.Move(moveDirection * Time.deltaTime);
//获得键盘输入
horizontal = Input.GetAxis("Horizontal");
//按重力落下
moveDirection.y -= gravity * Time.deltaTime;
//水平移动
if(horizontal > 0.01f)
{
moveDirection.x = horizontal * walkSpeed;
}
if(horizontal < 0.01f)
{
moveDirection.x = horizontal * walkSpeed;
}
//当角色在地面上时,按下空格键弹跳
if(characterController.isGrounded)
{
if(Input.GetKey(KeyCode.Space))
{
moveDirection.y = jumpHeight;
}
}
}
}
Unity3D游戏制作 Day1-实现角色的移动
最新推荐文章于 2022-03-23 22:37:05 发布