Skip to content

Instantly share code, notes, and snippets.

@ray5527880
Created February 24, 2018 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ray5527880/30f5bddc5121757c4b37da347bfd636c to your computer and use it in GitHub Desktop.
Save ray5527880/30f5bddc5121757c4b37da347bfd636c to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class walk : MonoBehaviour {
public GameObject mainCamera;
public Animator anim; //動畫控制器
public float Hor, Ver;//讀取方位值
public float play_rotion;//暫存方向
public attack attackCS;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!attackCS.attackSwitch)
{
walkCtrl();
}
}
void walkCtrl()
{
play_rotion = 0;
Hor = Input.GetAxis("Horizontal");//水平方位
Ver = Input.GetAxis("Vertical");//垂直方位
if ((Ver != 0) && (Hor != 0))//斜邊移動
{
play_rotion = mainCamera.transform.eulerAngles.y + (Mathf.Atan2(Hor, Ver) * Mathf.Rad2Deg);
}
else if (Ver > 0)//前
{
play_rotion = mainCamera.transform.eulerAngles.y;
}
else if (Ver < 0)//後
{
play_rotion = mainCamera.transform.eulerAngles.y + 180;
}
else if (Hor > 0)//右
{
play_rotion = mainCamera.transform.eulerAngles.y + 90;
}
else if (Hor < 0)//左
{
play_rotion = mainCamera.transform.eulerAngles.y + 270;
}
if ((Hor != 0) || (Ver != 0))//當有按方向鍵時
{
transform.eulerAngles = new Vector3(0, play_rotion, 0);//設定方向
anim.SetBool("Walk", true);//開啟動畫
transform.position = transform.position + transform.forward * Time.deltaTime;//座標移動
}
else
{
anim.SetBool("Walk", false);//關閉動畫
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment