zoukankan      html  css  js  c++  java
  • Unity3D 之3D游戏角色控制器运动

    3D运动,绑定了人形控制器后的一个简单的运动方法。

    using UnityEngine;
    using System.Collections;
    
    public class PlayerMove : MonoBehaviour {
    
        private CharacterController cc;
        private Animator animator;
        public float speed = 4;
    
        void Awake() {
            cc = this.GetComponent < CharacterController>();
            animator = this.GetComponent<Animator>();
        }
        
        
        // Update is called once per frame
        void Update () {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
    
            //按键的取值,以虚拟杆中的值为优先
            if (Joystick.h != 0 || Joystick.v != 0) {
                h = Joystick.h; 
                v = Joystick.v;
            }
    
            if (Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1) {
                animator.SetBool("Walk", true);
                if (animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerRun")) {
                    Vector3 targetDir = new Vector3(h, 0, v);
                    transform.LookAt(targetDir + transform.position);
                    cc.SimpleMove(transform.forward * speed);
                }
            } else {
                animator.SetBool("Walk", false);
            }
        }
    }

     复杂的运动,比如尝试着通过动力移动控制器。

    public float speed = 6.0F;
        public float jumpSpeed = 8.0F;
        public float gravity = 20.0F;
        private Vector3 moveDirection = Vector3.zero; 
           //一个复杂的运动
            if (cc.isGrounded)
            {
                moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection *= speed;
                if (Input.GetButton("Jump"))
                    moveDirection.y = jumpSpeed;
    
            }
            moveDirection.y -= gravity * Time.deltaTime;
            cc.Move(moveDirection * Time.deltaTime);

    可以完成人物贴在地面上运动的效果。

  • 相关阅读:
    CSS实现底部固定
    ES6新特性--多行文本
    DataTable转实体
    jQuery插件开发
    页面可编辑
    clearfix--清除浮动
    前端日历控件推荐
    图片Base64编码
    第八周学习进度博客
    人月神话多后感01
  • 原文地址:https://www.cnblogs.com/sunxun/p/5572755.html
Copyright © 2011-2022 走看看