2dAnimation
圖1
素材來源:Unity的Asset Store,Asset Store裡包含許多開源庫。
主要game的物件如右圖所示:
主要用到的指令碼有:
//author:劉家誠, last time:2020.10.15
private float x;
private float y;
public float speed = 5;
private Rigidbody2D _rigidbody_2D;
private Animator _animator;
// Start is called before the first frame update
void Start()
{
//2d的元件
_rigidbody_2D = GetComponent<Rigidbody2D>();
_animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
//正方向行走
if(x > 0)
{
_rigidbody_2D.transform.eulerAngles = new Vector3(0,0,0);
_animator.SetBool("run", true);
}
//反方向行走
if(x < 0)
{
_rigidbody_2D.transform.eulerAngles = new Vector3(0, 180, 0);
_animator.SetBool("run", true);
}
if(x < 0.001f && x > -0.001f)
{
_animator.SetBool("run", false);
}
Run();
}
private void Run()
{
Vector3 movement = new Vector3(x, y, 0);
_rigidbody_2D.transform.position += movement * speed * Time.deltaTime;
}
//player碰撞檢測用player做,並且要設定tag
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Spike")
{
//如果碰撞到障礙物,那麼player消失即可。並且呼叫GameController的ShowGameOverPanel方法
GameController.Instance.ShowGameOverPanel();
Destroy(gameObject);
}
}
`
專案網路硬碟連結地址,之後會傳到github上:連結:https://pan.baidu.com/s/1lxSTiGQdZ-0Ji6B0z2XMMw
提取碼:jc60
複製這段內容後開啟百度網路硬碟手機App,操作更方便哦–來自百度網路硬碟超級會員V3的分享