Unity代码笔记人物动作按键绑定+摄像机控制

在学习unity引擎很长一段时间之后,我想需要对引擎更加进一步理解。列出一些我认为很重要的代码,很基础,很实用。包含我个人理解,作为一些笔记。希望能够帮助到需要的人。

人物动作:

using UnityEngine;
using System.Collections;

public class AnimatorMove : MonoBehaviour {


    public float DirectionDampTime = 0.25f;
    private Animator animator;

    // Use this for initialization
    void Start () {
        animator = GetComponent();
    }

    // Update is called once per frame
    void Update () {
        //对是否得到相关的animator进行检测。
        if (animator == null)
        {
            return;
        }
        //用小面的方法进行动作状态机状态监测。
        AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
        if (stateInfo.IsName("Base Layer.Run"))
        {
            if (Input.GetButton("Fire1"))
            {
                animator.SetBool("jump", true);
            }
        }
        else
        {
            animator.SetBool("jump", false);
        }

        if (Input.GetButton("Fire2") && animator.layerCount >= 2)
        {
            animator.SetBool("hi", !animator.GetBool("Hi"));
        }

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        animator.SetFloat("speed", v*v+h*h);
        animator.SetFloat("direction", h, DirectionDampTime, Time.deltaTime);
    }

}

摄像机控制:

using UnityEngine;
using System.Collections;

public class CameraMove : MonoBehaviour {
    //GameObject to look at
    public Transform follow;
    public float distanceAway = 5.0f;
    public float distanceUp = 2.0f;
    public float smooth = 1.0f;
    private Vector3 targetPosition;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void LateUpdate()
    {
        //时刻计算出当前Camera应该在的位置。
        targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;
        transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime*smooth);
        transform.LookAt(follow);
    }




}

你可能感兴趣的:(Unity代码笔记)