// prg6-8 修正不能二段跳&死亡自動重新開始 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class PlayerController : MonoBehaviour { Rigidbody2D rigi2D; Animator animator; float jumpForce = 680.0f; float walkForce = 30.0f; float maxWalkSpeed = 2.0f; // Start is called before the first frame update void Start() { this.rigi2D = GetComponent(); this.animator = GetComponent(); } // Update is called once per frame void Update() { //Jump if(Input.GetKeyDown(KeyCode.Space) && this.rigi2D.velocity.y ==0) { this.rigi2D.AddForce(transform.up * this.jumpForce); } // Left Right Move int key = 0; if(Input.GetKey(KeyCode.RightArrow)) key = 1; if(Input.GetKey(KeyCode.LeftArrow)) key = -1; float speedx = Mathf.Abs(this.rigi2D.velocity.x); if(speedx < this.maxWalkSpeed) { this.rigi2D.AddForce(transform.right * key * this.walkForce); } if (key !=0) { transform.localScale = new Vector3(key, 1, 1); } this.animator.speed = speedx / 2.0f; if(transform.position.y < -10) { SceneManager.LoadScene("GameScene"); } } void OnTriggerEnter2D(Collider2D other) { Debug.Log("End"); SceneManager.LoadScene("ClearScene"); } }