63. Dart物件加上Dart程式腳本
public class Dart : MonoBehaviour{
//Use this for initialization
[SerializeField]
private float speed;
void Start(){
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0.0f);
}
//Update is called once per frame
void Update(){
}
private void OnBecameInvisible(){
Destroy(this.gameObject);
}
}
建立飛鏢預製物件 2/3
63
66. 在Ninja物件加上DartSpawner程式腳本
public class DartSpawner : MonoBehaviour{
[SerializeField]
private GameObject dart;
[SerializeField]
private Transform dartSpawn;
// Use this for initialization
void Start(){
}
// Update is called once per frame
void Update(){
}
public void ThrowDart(){
Instantiate(dart, dartSpawn.position, dartSpawn.rotation);
}
}
建立飛鏢發射控制器 2/3
66
75. 在Ninja物件加上NinjaController程式如下:
public class NinjaController : MonoBehaviour{
private Animator animator;
// Use this for initialization
void Start(){
animator = this.GetComponent<Animator>();
}
private void PlayAnimation(int action){
animator.SetInteger("action", action);
}
Lab 角色動畫控制 8/10
75
76. // Update is called once per frame
void Update(){
if (Input.GetKeyDown(KeyCode.Z)){
PlayAnimation(1);
} else if (Input.GetKeyDown(KeyCode.Space)){
PlayAnimation(2);
} else if (Input.GetKey(KeyCode.RightArrow)){
PlayAnimation(3);
} else if (Input.GetKeyDown(KeyCode.X)){
PlayAnimation(4);
}
else
PlayAnimation(0);
}
}
Lab 角色動畫控制 9/10
76