• 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

    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    Quartz使用总结(转)
    JAVA中使用LOG4J记录日志
    Java用HttpsURLConnection访问https网站的时候如何跳过SSL证书的验证?
    JAVA_HOME设置
    命令行启停mysql数据库
    本地jar包引入到maven项目中
    Could not clean server of obsolete files
    python基础一
    11-数据的增删改
    10-外键的变种 三种关系
  • 原文地址:https://www.cnblogs.com/plateFace/p/4686910.html
Copyright © 2020-2023  润新知