首页 > 代码库 > [Unity菜鸟] Character控制移动
[Unity菜鸟] Character控制移动
1. 给角色加角色控制器组件,然后用以下代码可以控制角色移动和跳跃
float speed = 6.0f; float jumpSpeed = 8.0f; float gravity = 20.0f; private Vector3 moveDirection = Vector3.zero; void Start() { // gameObject.rigidbody = false; } void Update() { CharacterController controller = GetComponent<CharacterController>(); if(controller.isGrounded) { moveDirection =new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //Allows for player input moveDirection = transform.TransformDirection(moveDirection); //How to move moveDirection *= speed; //How fast to move if(Input.GetButton("Jump")) { moveDirection.y = jumpSpeed; } } //Apply gravity moveDirection.y -= gravity * Time.deltaTime; //Move the controller controller.Move(moveDirection * Time.deltaTime); }
2. 添加角色控制器后人物下陷问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。