加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_开封站长网 (http://www.0378zz.com/)- 科技、AI行业应用、媒体智能、低代码、办公协同!
当前位置: 首页 > 教程 > 正文

Unity3D 一个设置趋势键移动和空格起跳的脚本

发布时间:2021-11-23 12:58:09 所属栏目:教程 来源:互联网
导读:Unity3D 一个设置方向键移动和空格起跳的脚本: /// This script moves the character controller forward /// and sideways based on the arrow keys. /// It also jumps when pressing space. /// Make sure to attach a character controller to the same

 Unity3D 一个设置方向键移动和空格起跳的脚本:
 
/// This script moves the character controller forward    
/// and sideways based on the arrow keys.   
/// It also jumps when pressing space.   
/// Make sure to attach a character controller to the same game object.   
/// It is recommended that you make only one call to Move or SimpleMove per frame.       
  
var speed : float = 6.0;  
var jumpSpeed : float = 8.0;  
var gravity : float = 20.0;  
  
private var moveDirection : Vector3 = Vector3.zero;  
  
function Update() {  
    var controller : CharacterController = GetComponent(CharacterController);  
    if (controller.isGrounded) {  
        // We are grounded, so recalculate   
        // move direction directly from axes   
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,  
                                Input.GetAxis("Vertical"));   
        // 这里获取了键盘的前后左右的移动,但注意,这是相对于自己的。   
        moveDirection = transform.TransformDirection(moveDirection);   
        // 还有一个TransformPoint。这里是把相对于自己的   
        // 坐标转换为相对于世界的坐标。   
        moveDirection *= speed;  
          
        if (Input.GetButton ("Jump")) {  
            moveDirection.y = jumpSpeed;  
        }  
    }  
  
    // Apply gravity   
    moveDirection.y -= gravity * Time.deltaTime;  
      
    // Move the controller   
    controller.Move(moveDirection * Time.deltaTime);  
}  
这段代码来自Unity3D的官方文档。

(编辑:开发网_开封站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读