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

    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    [转贴]35岁之前成功12条法则
    any type,any name
    The quick brown fox jumps over the lazy dog.
    [总结]软件工程师笔试题目(C++)
    [转]IOCP介绍
    A simple IOCP Server/Client Class
    Flash for Linux
    看看你是否需要更新SYMBOL文件了??
    [转贴]The Code Project Visual C++ Forum FAQ
    (搜集)一些少走弯路的话语+参考信息
  • 原文地址:https://www.cnblogs.com/plateFace/p/4686910.html
Copyright © 2011-2022 走看看