欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity实现见缝插针小游戏

程序员文章站 2023-09-06 15:08:27
本文实例为大家分享了unity实现见缝插针游戏的具体代码,供大家参考,具体内容如下控制小球旋转using system.collections;using system.collections.gen...

本文实例为大家分享了unity实现见缝插针游戏的具体代码,供大家参考,具体内容如下

控制小球旋转

using system.collections;
using system.collections.generic;
using unityengine;

public class rotateself : monobehaviour {
  //每秒旋转90度
  public float speed = 90;

  // update is called once per frame
  void update () {
   //绕z轴顺针旋转
    transform.rotate(new vector3(0, 0, -speed * time.deltatime));
  }
}

Unity实现见缝插针小游戏

针头碰撞检测

using system.collections;
using system.collections.generic;
using unityengine;

public class pinhead : monobehaviour {

  private void ontriggerenter2d(collider2d collision)
  {
    if (collision.tag == "pinhead")
    {
      gameobject.find("gamemanager").getcomponent<gamemanager>().gameover();
    }
  }
}

控制针的运动位置

using system.collections;
using system.collections.generic;
using unityengine;

public class pin : monobehaviour {

  public float speed = 5;
  private bool isfly = false;
  private bool isreach = false;
  private transform startpoint;
  private vector3 targetcirclepos;
  private transform circle;

  // use this for initialization
  void start () {
    startpoint = gameobject.find("startpoint").transform;
    circle = gameobject.findgameobjectwithtag("circle").transform;
    targetcirclepos = circle.position;
    targetcirclepos.y -= 1.55f;
  }

  // update is called once per frame
  void update () {
    if (isfly == false)
    {
      if (isreach == false)
      {
        transform.position = vector3.movetowards(transform.position, startpoint.position, speed * time.deltatime);
        if (vector3.distance(transform.position, startpoint.position) < 0.05f)
        {
          isreach = true;
        }
      }
    }
    else
    {
      transform.position = vector3.movetowards(transform.position, targetcirclepos, speed * time.deltatime);
      if(vector3.distance( transform.position,targetcirclepos) < 0.05f)
      {
        transform.position = targetcirclepos;
        transform.parent = circle;
        isfly = false;
      }
    }
  }

  public void startfly()
  {
    isfly = true;
    isreach = true;
  }
}

游戏管理

using system.collections;
using system.collections.generic;
using unityengine;
using unityengine.ui;
using unityengine.scenemanagement;

public class gamemanager : monobehaviour {

  private transform startpoint;
  private transform spawnpoint;
  private pin currentpin;
  private bool isgameover = false;
  private int score = 0;
  private camera maincamera;
  public text scoretext;
  public gameobject pinprefab;
  public float speed = 3;

  // use this for initialization
  void start () {
    startpoint = gameobject.find("startpoint").transform;
    spawnpoint = gameobject.find("spawnpoint").transform;
    maincamera = camera.main;
    spawnpin();
  }

  private void update()
  {
    if (isgameover) return;
    if (input.getmousebuttondown(0))
    {
      score++;
      scoretext.text = score.tostring();
      currentpin.startfly();
      spawnpin();
    }
  }

  void spawnpin()
  {
     //针的实例化
    currentpin = gameobject.instantiate(pinprefab, spawnpoint.position, pinprefab.transform.rotation).getcomponent<pin>();
  }

  public void gameover()
  {
    if (isgameover) return;
    gameobject.find("circle").getcomponent<rotateself>().enabled = false;
    startcoroutine(gameoveranimation());
    isgameover = true;
  }

  ienumerator gameoveranimation()
  {
    while (true)
    {
      maincamera.backgroundcolor = color.lerp(maincamera.backgroundcolor, color.red, speed * time.deltatime);
      maincamera.orthographicsize = mathf.lerp(maincamera.orthographicsize, 4, speed * time.deltatime);
      if( mathf.abs( maincamera.orthographicsize-4 )<0.01f)
      {
        break;
      }
      yield return 0;
    }
    yield return new waitforseconds(0.2f);
    //重新加载场景
    scenemanager.loadscene(scenemanager.getactivescene().buildindex);
  }
}

游戏初始状态和运行结果

Unity实现见缝插针小游戏

Unity实现见缝插针小游戏

更多有趣的经典小游戏实现专题,分享给大家:

c++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

javascript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

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