Unity控制人物移動和移動動畫
阿新 • • 發佈:2019-02-09
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System .Serializable ]
public class Anim//遊戲控制動畫
{
public AnimationClip idle;
public AnimationClip runForward;
public AnimationClip runBackward;
public AnimationClip runRight;
public AnimationClip runleft;
}
public class playermove : MonoBehaviour {
public float h = 0.0f;
public float v = 0.0f;
//分配變數,
private Transform tr;
//移動速度變數
public float movespeed = 10.0f;
//旋轉可使用Rotate函式,
public float rotSpeed = 100.0f;
//要顯示到檢視檢視的動畫類變數
public Anim anim;
//要訪問下列3d模型animation元件物件的變數
public Animation _animation;
// Use this for initialization
void Start () {
//向指令碼初始部分分配Tr元件
tr = GetComponent<Transform>();
//查詢位於自身下級的anim元件並分配到變數。
_animation = GetComponentInChildren<Animation>();
_animation.clip = anim.idle;
_animation.Play();
}
// Update is called once per frame
void Update () {
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
Debug.Log("H=" + h.ToString());
Debug.Log("V="+ v.ToString());
//計算左右前後的移動方向向量。
Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h);
//translate(移動方向*time.deltatime*movespeed,space.self)
tr.Translate(moveDir.normalized *Time.deltaTime*movespeed , Space.Self);
//vector3.up軸為基準,以rotspeed速度旋轉
tr.Rotate(Vector3.up * Time.deltaTime * rotSpeed * Input.GetAxis("Mouse X"));
if (v >= 0.1f)
{
//前進動畫
_animation.CrossFade(anim.runForward.name, 0.3f);
}
else if (v <= -0.1f)
{
//back animation
_animation.CrossFade(anim.runBackward.name, 0.3f);
}
else if (h >= 0.1f)
{
//right animation
_animation.CrossFade(anim.runRight.name, 0.3f);
}
else if (h <= -0.1f)
{
//left animation
_animation.CrossFade(anim.runleft.name, 0.3f);
}
else
{
_animation.CrossFade(anim.idle.name, 0.3f);
}
//以鍵盤輸入值為基準,執行要操作的動畫
}
}
using System.Collections.Generic;
using UnityEngine;
[System .Serializable ]
public class Anim//遊戲控制動畫
{
public AnimationClip idle;
public AnimationClip runForward;
public AnimationClip runBackward;
public AnimationClip runRight;
public AnimationClip runleft;
}
public class playermove : MonoBehaviour {
public float h = 0.0f;
public float v = 0.0f;
//分配變數,
private Transform tr;
//移動速度變數
public float movespeed = 10.0f;
//旋轉可使用Rotate函式,
public float rotSpeed = 100.0f;
//要顯示到檢視檢視的動畫類變數
public Anim anim;
//要訪問下列3d模型animation元件物件的變數
public Animation _animation;
// Use this for initialization
void Start () {
//向指令碼初始部分分配Tr元件
tr = GetComponent<Transform>();
//查詢位於自身下級的anim元件並分配到變數。
_animation = GetComponentInChildren<Animation>();
_animation.clip = anim.idle;
_animation.Play();
}
// Update is called once per frame
void Update () {
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
Debug.Log("H=" + h.ToString());
Debug.Log("V="+ v.ToString());
//計算左右前後的移動方向向量。
Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h);
//translate(移動方向*time.deltatime*movespeed,space.self)
tr.Translate(moveDir.normalized *Time.deltaTime*movespeed , Space.Self);
//vector3.up軸為基準,以rotspeed速度旋轉
tr.Rotate(Vector3.up * Time.deltaTime * rotSpeed * Input.GetAxis("Mouse X"));
if (v >= 0.1f)
{
//前進動畫
_animation.CrossFade(anim.runForward.name, 0.3f);
}
else if (v <= -0.1f)
{
//back animation
_animation.CrossFade(anim.runBackward.name, 0.3f);
}
else if (h >= 0.1f)
{
//right animation
_animation.CrossFade(anim.runRight.name, 0.3f);
}
else if (h <= -0.1f)
{
//left animation
_animation.CrossFade(anim.runleft.name, 0.3f);
}
else
{
_animation.CrossFade(anim.idle.name, 0.3f);
}
//以鍵盤輸入值為基準,執行要操作的動畫
}
}