码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D 之3D游戏运动

时间:2016-06-09 22:08:57      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:

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);
        }
    }
}

 

Unity3D 之3D游戏运动

标签:

原文地址:http://www.cnblogs.com/sunxun/p/5572755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!