Unity 学习第二天

125 阅读1分钟

using System.Collections;

using System.Collections.Generic; using UnityEngine

public class player : MonoBehaviour{

   //记录 x,z虚拟轴

private float x;
private float z;
private float speed;
// Start is called before the first frame update
void Start()
{

}

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

    //获取x,z虚拟轴 我们知道获取到的参数 让物体朝这个方向一点
    x = Input.GetAxis("Horizontal");
    z = Input.GetAxis("Vertical");

    Vector3 dir = new Vector3(x, 0, z);//获取物体方向

    speed = Mathf.Sqrt(x * x + z * z) * 10;

    //判断当前是否按下按键
    if (dir != Vector3.zero)
    {
        transform.rotation = Quaternion.LookRotation(dir);//让物体旋转方向和dir保持一致
                                                          //接下来是往前移动
        transform.Translate(Vector3.forward * Time.deltaTime * speed);//每秒一点一格

    }
  }
 }

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

    //炸弹身上的特点  一开始没有出现 玩家点击某个键释放  倒计时 删除自身 爆炸 爆炸特效删除
    //倒计时 不能被代码阻塞 线程 协程 异步运行

public class boom : MonoBehaviour {

public int time = 3;
private TextMeshPro timeText;
void Start()
{

    timeText = GetComponentInChildren<TextMeshPro>();
    timeText.text = time.ToString();
    timeText.color = Color.red;

    StartCoroutine(load());

}

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


}
IEnumerator load()
{

    while (true)
    {
        yield return new WaitForSeconds(1f);
        time--;
        timeText.text = time.ToString();
        timeText.color = Color.red;
        if (time <= 0)
        {
            yield break;
        }
    }
}

}