Unity2D实现游戏回旋镖

 更新时间:2021年9月13日 00:00  点击:1608

本文实例为大家分享了Unity2D游戏回旋镖实现的具体代码,供大家参考,具体内容如下

以下我举出2种同使用情况的回旋镖

那么回旋镖需要怎么做呢?

任务清单如下

  • 回旋镖会自我旋转
  • 回旋镖达到一定距离会飞回来
  • 回旋镖对敌人造成伤害
  • 回旋镖会飞回玩家手里

带着这些任务我们来依次实现这两种情况的回旋镖吧

1、在2D平面游戏上的回旋镖

实现一个丢出回旋镖后,会缓慢减速,然后再直接收回手里,碰到怪马上返回的效果
先创建一个回旋镖实体,并在上面添加如下脚本

public float Speedrotate;
public float AttackDamage;
   
    private Vector2 speedVelocity;
    private float Speedtemprotate;//中间变量记录最大旋转速度
    
    bool first = true;//此bool值代表是否是第一阶段:即丢出去的飞行阶段
    void Start()
    {
        speedVelocity = gameObject.GetComponent<Rigidbody2D>().velocity;
        Speedtemprotate = Speedrotate;
    }
    private void FixedUpdate()
    {
     transform.Rotate(0, 0, Speedrotate);//根据旋转速度来自转
     if(Speedrotate > 0 && first)//旋转速度和飞行随时间减小,直到为0就停止运动
     {
         gameObject.GetComponent<Rigidbody2D>().velocity -= speedVelocity * 0.02f;
            Speedrotate -= (Speedtemprotate * 0.02f);
     }
     else if(Speedrotate<0&&first)//第一阶段结束开始过渡第二阶段
     {
      speedVelocity = gameObject.GetComponent<Rigidbody2D>().velocity =      (GameObject.Find("Player").transform.position - transform.position).normalized;
      //获得新的速度(因为玩家可能会移动到任何位置,所以不能只是单纯往回飞)
             first = false;//第二阶段开始
     }
     else if(!first)//第二阶段回旋镖一直往玩家方向飞行
     {
         Speedrotate += (Speedtemprotate * 0.02f);//这个0.02f速度可以调快一点看个人喜爱
            float x = GameObject.Find("Player").transform.position.x;
            float y = GameObject.Find("Player").transform.position.y;
            Vector2 dir = new Vector2(x - transform.position.x, y - transform.position.y).normalized * Time.deltaTime;
            transform.Translate(dir * Time.fixedDeltaTime * 500,Space.World);
     }
    }
     private void OnTriggerEnter2D(Collider2D collision)
     {
      if (collision.tag == "Enemy" && first)
      {
          first = false;
            Speedrotate = 0;//强行结束first阶段
            
            other.GetComponent<Enemy>.TakeDamage(damage);//对敌人造成伤害
      }
      
}

然后在玩家身上加上脚本,这里逻辑是根据鼠标位置发射回旋镖

private void Update()
{
 if (!isCanweapon) TimeweaponBack += Time.deltaTime;//获取回旋镖返回时间
        TimeweaponShoot += Time.deltaTime;//设置回旋镖冷却
 //发射回旋镖
 if(TimeweaponShoot>=AttackSpeed&&Input.GetAxis("Fire2")==1&&isCanweapon)
 {
     TimeweaponBack = 0;
            isCanweapon = false;
            Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);//获取当前屏幕的点
            Vector3 dir = worldPos - transform.position;//获取玩家与屏幕的方向
            dir.z = 0;//2D游戏不需要z轴
            GameObject go = Instantiate(weapon, transform.position+dir.normalized*0.2f, transform.rotation);
            go.GetComponent<Rigidbody2D>().velocity = dir.normalized*4;//给回旋镖速度
        
 }
}
 private void OnTriggerEnter2D(Collider2D collision)
 {
  //捡返回的回旋镖
        if(collision.tag=="weapon"&& TimeweaponBack > 0.03f)
        {
            isCanweapon = true;
            TimeweaponShoot = 0;
            Destroy(collision.gameObject);
        }
 }

效果演示如下:

2、只在左右面上回旋镖(转运B站up秦无邪的)

实现一个回旋镖丢出然后飞回,中途不会被怪打断飞行,并且y轴跟随玩家

代码如下

void Start()
{
 rb2D=GetComponent<Rigidbody2D>();
 
 rb2D.velocity=transform.right*speed;//给回旋镖初始速度
 
 startSpeed=rb2D.velocity;//记录初始速度
 
 playerTransform=GameObject.FindGameObjectWithTag("Player").GetComponment<Transform>();//获得玩家位置
 void Update()
{
 transform.Rotate(0,0,rotateSpeed);//自我旋转
 
 float y=Mathf.Lerp(transform.position.y,playerTransform.position.y,0.1);//让回旋镖y轴和玩家在一起
 transform.position=new Vector3(transform.position.x,y,0.0f);
 
 rb2D.velocity-=startSpeed*Time.deltaTime;//使回旋镖慢慢减速,到达0之后速度方向为之前的反方向飞回来
 
 if(Mathf.Abs(transform.position.x-playerTransform.position.x)<0.5f)
  {
    Destory(gameObject);//飞回玩家手里
  }
}

}
void OnTriggerEnter2D(Collider2D other)
{
 if(other.gameObject.CompareTag("Enemy"));
 {
  other.GetComponent<Enemy>.TakeDamage(damage);//对敌人造成伤害 
 }
}

最后新健一个发射器绑在玩家的前面(注意是前面,如果在玩家身体上可能会一释放就会被Destory)

按下按键生成即可

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • Unity2D实现游戏回旋镖

    这篇文章主要为大家详细介绍了Unity2D实现游戏回旋镖,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-13