unity之人物行走的实现


            在3D游戏中,人物的行走是非常频繁的。但也有很多种不同的方式。


                 1:A,D键只控制旋转,W,S键控制向前进,还是向后退。适合第一人称的控制运动。


                                        

using UnityEngine;
using System.Collections;

public class PeopleMove : MonoBehaviour {

    public float speed = 3.0F;//速度

    public float rotateSpeed = 3.0F;
    private Transform tran;
    private Animator m_animator;
    CharacterController controller;
    
	void Start () {
        tran = gameObject.GetComponent();
        m_animator = gameObject.GetComponent();
        controller = GetComponent();
	}

	void Update () {

        float h = Input.GetAxis("Horizontal");//返回-1到1的实数值,可以来构造向量
        float v = Input.GetAxis("Vertical");//竖直方向

        transform.Rotate(0, h * rotateSpeed, 0);//AD键水平旋转

        Vector3 forward = transform.TransformDirection(Vector3.forward);//这是将世界坐标系变为局部坐标系,和controller.SimpleMove(forward * curSpeed);
        //对应不然人转向之后,向前走还是侧起走

        float curSpeed = speed *v;

        //竖直方向,按下WS键,人物播放向前走动画,并前进。
        if (Mathf.Abs(v) > 0.1)//判断是否按下键WS
        {
            //第一种方法,用SimpleMove
            controller.SimpleMove(forward * curSpeed);//向前运动,由于变换了坐标系所以旋转之后,也可以往前走
            //第二种方法,用Translate
            //if (Input.GetKey(KeyCode.W))
            //    tran.Translate(Vector3.forward * 0.5f);
            //else if (Input.GetKey(KeyCode.S))
            //    tran.Translate(Vector3.back * 0.5f);
            m_animator.SetBool("walk", true);//播放行走动画
        }
        else
        {
            m_animator.SetBool("walk", false);
        }
       
}


         2:第二种走的方式是,按W键就朝向初始正面的左方向前进,(按A键左转,再按A不会再左转,而是向左前进),按W就往初始方向的前方移动。这种方式适合第三人称的控制运动。

               

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public float speed = 3.0F;//速度
    public float moveSpeed = 1;
    public float stopSpeed = 20;
    public float rotateSpeed = 0;
    public bool hasKey = false;
    private CharacterController controller;

    private Animator anim;

    void Start()
    {
        controller = gameObject.GetComponent();
    }


    void Awake() {
        anim = this.GetComponent();//获取动画组件
    }

    void Update() {
        

        float h = Input.GetAxis("Horizontal");//获取轴
        float v = Input.GetAxis("Vertical");

        if (Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1) //按下wsad键
        {
            float newSpeed = Mathf.Lerp(anim.GetFloat("Speed"), 10f, moveSpeed * Time.deltaTime);
            anim.SetFloat("Speed", newSpeed);//原来是这句让人物前进的,少了这一句,人物在按A之后就旋转到起始方向的左方向,再按A不会再向左旋转,因为人物已经面向启示方向的左方向,就会一直向左前进。

            Vector3 targetDir = new Vector3(h, 0, v);

            Quaternion newRotation = Quaternion.LookRotation(targetDir, Vector3.up);//创建旋转
            transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
        } 
        else {
            anim.SetFloat("Speed", 0);
        }

}//unity官方教程秘密潜行游戏

          3:

移动物体的方法:
1:通过transform:private Transform tran;
tran.Translate(Vector.back,);
2:通过rigidbody。
private Rigidbody rigi;
rigi.Moveposition(transform.position+Vector.back);

         4:角色控制器和刚体都可以移动角色,区别是,刚体他灵活了,有什么反弹力啊,跟物理相关的属性有很多,而角色控制器,只是控制移动角色

         5:利用动画状态树控制人物行走的简单实现: unity之人物行走的实现_第1张图片
其中Blend Tree是左右转,直走,三种动画状态构成的状态树。Speed用于idle到状态树的切换,Direction是状态树中各动画片段的切换。但脚本控制中利用了简便的方法:
using UnityEngine;
using System.Collections;

public class AnimationS : MonoBehaviour {

    private Animator ani;
	void Start () {
        ani = GetComponent();
	}
	void Update () {
        ani.SetFloat("Speed", Input.GetAxis("Vertical"));
        ani.SetFloat("Direction", Input.GetAxis("Horizontal"));
	}
}


        

你可能感兴趣的:(Unity,Animation,unity)