一個demo(角色控制器中的),添加一些邏輯。
點擊角色,減速後退。
協同程序 用來角色減速的
IEnumerator move(){
float speed=5f;
for(int i=1;i<100;i++){
_velocity.x = speed *x ;
speed -= 0.05f;
yield return new WaitForSeconds(0.02f); //每0.02秒執行一次,共執行 100次
}
}
StartCoroutine ("move"); //協同程序的調用
點擊角色
3D 射線檢測 檢測不了帶有boxcollider2d 的物件 ,弄了2個小時。。。- -,嘛畢竟連射線檢測最開始都不明白。。。
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log("click object name is " + gameObject.name);
}
}
2D射線檢測。。。
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null)
{
Debug.Log("click object name is " + gameObject.name);
}
}