zoukankan      html  css  js  c++  java
  • CharacterController 角色控制器实现移动和跳跃

    之前我使用SimpleMove来控制角色的移动, 后来又想实现人物的跳跃, 看见圣典里面是使用Move来实现的. =.= 然后我都把他们改成move来实现了

    代码实现:

    using UnityEngine;
    using System.Collections;
    
    public class PlayerMove : MonoBehaviour {
    
    
        private CharacterController cc;
        private bool isJump;
        private bool isMove;
        public float moveSpeed = 4;             //移动的速度
        public float jumpSpeed = 4;             //跳跃的速度
        public float gravity = 1;               //重力
    
        private Vector3 moveDirection;
    
        private float h = 0;
        private float v = 0;
        private Vector3 targetDir;
        private CollisionFlags flags;
        void Start () {
            cc = this.GetComponent<CharacterController>();          
        }
        
        void Update () {
            h = Input.GetAxis("Horizontal");
            v = Input.GetAxis("Vertical");
    
            if (Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f)
            {
                targetDir = new Vector3(h, 0, v);
                transform.LookAt(targetDir + transform.position);
                isMove = true;
            }
    
            if (Input.GetButton("Jump") && !isJump)
            {
                isJump = true;
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection.y = jumpSpeed;
            }
    
            if (isJump)
            {
                //模拟物理,开始下降
                moveDirection.y -= gravity * Time.deltaTime;
                flags =  cc.Move(moveDirection * Time.deltaTime);
    
                //人物碰撞到下面了
                if (flags == CollisionFlags.Below) 
                {
                    isJump = false;
                }
            }
    
            if (isMove)
            {
                cc.Move(transform.forward * moveSpeed * Time.deltaTime);
                isMove = false;
            }
     
        }
    }

    image

    Unity5.1

    下载地址: http://yunpan.cn/ccTGZuZI5sc5J  访问密码 c0b3

    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    MVC ActionResult JsonResult
    xml文件
    使用socket实现聊天功能
    使用多线程完成Socket
    Socket编程
    U1总结
    多线程
    IO
    单例模式
    日期
  • 原文地址:https://www.cnblogs.com/plateFace/p/4686910.html
Copyright © 2011-2022 走看看