123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour {
- private Animation anim;
- public float turnSmoothing = 15f;
- public GameObject Bomb;
- public Transform myTransform;
- public float moveSpeed = 3f;
- public bool IsDead = false;
- public GameController gamecontroller;
- private Rigidbody rigidBody;
- public int playerNumber = 1;
- public int playerbomb = 2;
- public int lifenum = 2;
- void Awake () {
- anim = GetComponent <Animation>();
- myTransform = GetComponent<Transform> ();
- rigidBody = GetComponent<Rigidbody> ();
- }
- void Update () {
-
- }
- void Rotating(float h,float v){
- Vector3 targetDir = new Vector3(h,0,v);
- Quaternion targetRotation = Quaternion.LookRotation (targetDir, Vector3.up);
- Rigidbody r = GetComponent<Rigidbody> ();
- Quaternion newRotation = Quaternion.Lerp (r.rotation, targetRotation, turnSmoothing * Time.deltaTime);
- r.MoveRotation (newRotation);
- }
- void MoveManagement(float h,float v){
-
- if (h != 0 || v != 0) {
- Rotating (h,v);
- Vector3 speed = new Vector3(h,0,v);
- rigidBody.MovePosition (rigidBody.position + moveSpeed * speed * Time.deltaTime);
-
-
- }
- }
- void FixedUpdate(){
- if (playerNumber == 1) {
- float h = Input.GetAxis ("Horizontal");
- float v = Input.GetAxis ("Vertical");
- anim.Play("ghost_idle_hover");
- MoveManagement (h, v);
- if (Input.GetKeyDown ("f")) {
- DropBomb();
- anim.Play("ghost_attack_with_ball");
- }
- }
- else
- {
- float h = Input.GetAxis ("Fire1");
- float v = Input.GetAxis ("Fire2");
- anim.Play("dragon_dizzy");
- MoveManagement (h, v);
- if (Input.GetKeyDown ("l")) {
- DropBomb();
- anim.Play("dragon_bite");
- }
- }
-
- }
- private void DropBomb() {
- if (Bomb) {
- Bomb.GetComponent<Bomb> ().explosionLength = playerbomb;
- Vector3 BombPos = new Vector3 (Mathf.RoundToInt (myTransform.position.x),
- 0, Mathf.RoundToInt (myTransform.position.z));
- Instantiate(Bomb, BombPos,Bomb.transform.rotation);
- }
- }
- public void OnTriggerEnter(Collider other) {
- if (other.CompareTag ("Explosion")) {
- if (lifenum == 1) {
- lifenum = lifenum - 1;
- Debug.Log ("P" + playerNumber + "hit by explosion!");
- IsDead = true;
- gamecontroller.playerDied (playerNumber);
- Destroy (gameObject);
- } else {
- lifenum = lifenum - 1;
- }
- }
- if (other.CompareTag ("BombLength")) {
- playerbomb = playerbomb + 1;
- }
- if (other.CompareTag ("lifecube")) {
- lifenum = lifenum + 1;
- }
- if (other.CompareTag ("SpeedCube")) {
- moveSpeed = moveSpeed + 0.5f;
- }
- }
- }
|