子弹射出
完成僵尸的攻击逻辑后,本次目的是完成豌豆射手子弹的的效果,以便后续实现僵尸的受击效果
加入碰撞和刚体效果
给子弹选择collider 2D和rigidbody 2D,同理僵尸也加入碰撞。
实现子弹的旋转和移动
public class Bullet : MonoBehaviour
{
private Rigidbody2D rigidbody;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
rigidbody.AddForce(Vector2.right * 300);
}
void Update()
{
transform.Rotate(new Vector3(0, 0, -15f));
}
}
实现子弹与僵尸碰撞后的效果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName ="GameConf",menuName ="GameConf")]
public class GameConf : ScriptableObject
{
[Tooltip("阳光")]
public GameObject Sun;
[Tooltip("太阳花")]
public GameObject SunFlower;
[Tooltip("豌豆射手")]
public GameObject PeaShooter;
[Header("子弹")]
[Tooltip("豌豆击中")]
public Sprite BulletHit;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private Rigidbody2D rigidbody;
private SpriteRenderer spriteRenderer;
private bool isHit=false;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
rigidbody.AddForce(Vector2.right * 300);
spriteRenderer = GetComponent<SpriteRenderer>();
}
private void OnTriggerEnter2D(Collider2D coll)
{
if (isHit) return;
if (coll.tag == "Zombie")
{
isHit = true;
spriteRenderer.sprite = GameManager.Instance.GameConf.BulletHit;
rigidbody.velocity = Vector2.zero;
rigidbody.gravityScale = 1;
Invoke("Destroy", 0.5f);
}
}
private void Destroy()
{
Destroy(gameObject);
}
void Update()
{
if (isHit) return;
transform.Rotate(new Vector3(0, 0, -15f));
}
}
|