unity做单机游戏,用Rigidbody控制角色移动与朝向问题

unity做单机游戏,用Rigidbody控制角色移动与朝向问题(毕设过程中的一些心得)

Vector3 nowVel = rig.velocity;
rig.velocity = new Vector3 (velocity * h, nowVel.y, velocity * v); //移动
transform.LookAt (new Vector3 (h, 0, v) + transform.position); //朝向

`这是个人做的项目中的移动代码,最好看看有选择性的复制,并不是所有的东西都是你需要的,只是做一个例子,多余的就不解释了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour {
    public  float velocity = 5; //速度
    private Rigidbody rig;
    private Animator anim;

    // Use this for initialization
    void Start () {
        rig = this.GetComponent();
        anim = this.GetComponent();
    }

    // Update is called once per frame
    void Update () {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 nowVel = rig.velocity;
        if (Mathf.Abs(h) > 0.05f || Mathf.Abs(v) > 0.05f)
        {
            rig.isKinematic = false;
            anim.SetBool("Move", true);
            if (anim.GetCurrentAnimatorStateInfo (1).IsName ("Empty State")) {
                rig.velocity = new Vector3 (velocity * h, nowVel.y, velocity * v);
                transform.LookAt (new Vector3 (h, 0, v) + transform.position);

            } else {
                rig.velocity = new Vector3(0, nowVel.y, 0);

            }
        }
        else {
            rig.isKinematic = true;
            anim.SetBool("Move", false);
            rig.velocity = new Vector3(0, nowVel.y, 0);
        }
    }
}

“`

你可能感兴趣的:(unity做游戏中的一些心得)