2dAnimation遊戲
`//author:劉家誠:date:2020.10.16
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠:date:2020.10.16
public class PlayerController : MonoBehaviour
{
//控制角色的移動,生命,動畫等
public float speed = 5f;//移動速度
private int maxHealth = 5;//最大生命值
private int currentHealth;//當前生命值
private float invicibleTime = 2f;//無敵時間
private float invincibleTimer;//無敵計時器
private bool isInvincible;//是否無敵
public GameObject bulletPrefab;//子彈
//=====玩家的朝向=====
private Vector2 lookDirection = new Vector2(1, 0);
//不希望公開,但希望存取屬性值
public int MyMaxHealth
{
get
{
return maxHealth;
}
}
public int MyCurrentHealth
{
get
{
return currentHealth;
}
}
Rigidbody2D rbody;
Animator anim;
// Start is called before the first frame update
void Start()
{
//獲取剛體元件
rbody = GetComponent<Rigidbody2D>();
currentHealth = 2;//初始生命值
invincibleTimer = 0;
rbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//Time.deltaTime是計算機渲染一幀所需時間
float moveX = Input.GetAxisRaw("Horizontal");//控制水平移動方向A:-1,D:1
float moveY = Input.GetAxisRaw("Vertical");//控制水平移動方向w:1,s:-1
Vector2 moveVector = new Vector2(moveX, moveY);
//防止鬆手朝向改變
if (moveVector.x != 0 || moveVector.y != 0)
{
lookDirection = moveVector;
}
anim.SetFloat("Look X", lookDirection.x);
anim.SetFloat("Look Y",lookDirection.y);
anim.SetFloat("Speed", moveVector.magnitude);//根據向量大小來判斷
//=========移動==========
Vector2 position = rbody.position;
//position.x += moveX * speed * Time.deltaTime;
// position.y += moveY * speed * Time.deltaTime;
position += moveVector * speed * Time.deltaTime;
rbody.MovePosition(position);//實現剛體的移動
//==========無敵記時==========
if (isInvincible)
{
invincibleTimer -= Time.deltaTime;
if (invincibleTimer < 0)
{
isInvincible = false;//記錄時間,2s後無敵時間消失
}
}
//========按下j鍵進行攻擊=========
if (Input.GetKeyDown(KeyCode.J))
{
anim.SetTrigger("Launch");
GameObject bullet = Instantiate(bulletPrefab, rbody.position+Vector2.up*0.5f, Quaternion.identity);
BulletController bc = bullet.GetComponent<BulletController>();
if (bc != null)
{
bc.Move(lookDirection, 300);
}
}
}
//改變玩家生命值
public void ChangeHealth(int amount)
{
//收到傷害
if (amount < 0)
{
if (isInvincible == true)//如果是無敵,則退出
{
return;
}
isInvincible = true;//變成無敵
invincibleTimer = invicibleTime;
}
Debug.Log(currentHealth + "/" + maxHealth);
//用Mathf約束一下
currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
Debug.Log(currentHealth + "/" + maxHealth);
}
//與玩家的碰撞檢測
}
```csharp
在這裡插入程式碼片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠:date:2020.10.16
//敵人控制相關
public class EnemyController : MonoBehaviour
{
private float speed = 7;
private Rigidbody2D rbody;
//是否垂直
public bool isVertical;
public float changeDirectionTime = 2;
private float changeTimer;//計時器
private Vector2 moveDirection;//移動方向
private Animator anim;
private bool isFixed;//是否被修復
// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();//獲取剛體
moveDirection = isVertical ? Vector2.up : Vector2.right;//判斷是否垂直
changeTimer = changeDirectionTime;
anim = GetComponent<Animator>();//獲取anim
isFixed = false;
}
// Update is called once per frame
void Update()
{
//被修復,不執行以下所有程式碼
if (isFixed) return;
changeTimer -= Time.deltaTime;
if (changeTimer < 0)
{
moveDirection *= -1;
changeTimer = changeDirectionTime;
}
Vector2 position = rbody.position;
position.x += moveDirection.x * speed * Time.deltaTime;
position.y += moveDirection.y * speed * Time.deltaTime;
rbody.MovePosition(position);
anim.SetFloat("movex", moveDirection. x);
anim.SetFloat("movey", moveDirection.y);
}
private void OnCollisionEnter2D(Collision2D collision)
{
PlayerController pc = collision.gameObject.GetComponent<PlayerController>();
if (pc != null)
{
pc.ChangeHealth(-1);
}
}
public void Fixed()
{
isFixed = true;
rbody.simulated = false;
anim.SetTrigger("fix");//播放被修復的動畫
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠:date:2020.10.16
/// <summary>
/// 控制子彈的移動
/// </summary>
public class BulletController : MonoBehaviour
{
Rigidbody2D rbody;
// Start is called before the first frame update
void Awake()
{
rbody = GetComponent<Rigidbody2D>();
Destroy(this.gameObject, 2f);
}
// Update is called once per frame
void Update()
{
}
public void Move(Vector2 moveDirection,float moveForce)
{
rbody.AddForce(moveDirection * moveForce);
}
//=====碰撞檢測=====
private void OnCollisionEnter2D(Collision2D collision)
{
EnemyController ec = collision.gameObject.GetComponent<EnemyController>();
if (ec != null)
{
Debug.Log("碰到敵人了");
ec.Fixed();//修復敵人
}
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠:date:2020.10.16
public class Dangerous : MonoBehaviour
{
//傷害陷阱
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnTriggerStay2D(Collider2D collision)
{
PlayerController pc = collision.GetComponent<PlayerController>();
if (pc != null)
{
pc.ChangeHealth(-1);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠:date:2020.10.16
public class CollectAble : MonoBehaviour
{
//草莓被玩家碰撞時檢測的相關類
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
//檢測到有PlayerController
PlayerController pc = collision.GetComponent<PlayerController>();
if (pc != null)
{
if (pc.MyCurrentHealth < pc.MyMaxHealth) {
pc.ChangeHealth(1);
Destroy(this.gameObject);
}
}
}
}