最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity實(shí)現(xiàn)3D貪吃蛇的移動(dòng)代碼

 更新時(shí)間:2020年04月16日 14:27:32   作者:永遠(yuǎn)的小白蝦  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)3D貪吃蛇的移動(dòng)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Unity實(shí)現(xiàn)3D貪吃蛇移動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下

記錄一下前段時(shí)間寫(xiě)到的一個(gè)3D貪吃蛇的移動(dòng)代碼。

鏈接:Unity實(shí)現(xiàn)3D貪吃蛇

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class GameManager : MonoBehaviour
{
 List<Transform> bodyList = new List<Transform>();//身體位置的列表
 private float speed = 2f;//移動(dòng)速度
 public GameObject bodyObj;//用來(lái)生成的身體對(duì)象
 List<Transform> bodyCreatePostion = new List<Transform>();//身體部分的transfrom 列表
 private float TransOffset=1f;//位置間隔
 private List<Vector2> mapAddress=new List<Vector2>();//前進(jìn)目標(biāo)在地圖上的位置,
 public Transform firstBody;//第一個(gè)身體部件的transfrom
 const int row = 100;//地圖寬
 const int col = 100;//地圖高
 int step=1;//步伐
 int x=0, z=0;//記錄x和z軸的移動(dòng)量

 // Start is called before the first frame update
 public Vector3[,] map = new Vector3[row, col];//地圖
 // Start is called before the first frame update
 void Start()
 {
  
  for (int i = 0; i < row; i++)//生成地圖
  {
   for (int j = 0; j < col; j++)
   {
    map[i, j] = new Vector3(i, 1.5f, j);
   }
  }
  transform.position =map[0,0];將當(dāng)前位置設(shè)為原點(diǎn)
  mapAddress.Add(new Vector2(1,0));//增加第一個(gè)目標(biāo)
  bodyList.Add(transform);//刷新第一個(gè)頭和一個(gè)身體的transform
  bodyList.Add(firstBody);
  mapAddress.Add(new Vector2(0,0));
 }

 // Update is called once per frame
 void Update()
 {
  for (int i = 0; i < row - 1; i++)
  {
   for (int j = 0; j < col - 1; j++)//繪制地圖格子
   {
    Debug.DrawLine(map[i, j], map[i + 1, j], Color.red);
    Debug.DrawLine(map[i, j], map[i, j + 1], Color.red);
   }
  }
  if (Input.anyKeyDown)//有任何鍵按下
  {
   if (Input.GetKeyDown(KeyCode.W) && z != -step)//上
   {
    z = step;
    x = 0;


   }
   if (Input.GetKeyDown(KeyCode.S) && z != step)//下
   {
    z = -step;
    x = 0;
    
   
   }
   if (Input.GetKeyDown(KeyCode.A) && x != step)//左
   {
    x = -step;
    z = 0;

   
   }
   if (Input.GetKeyDown(KeyCode.D) && x != -step)//右
   {
    x = step;
    z = 0;
   
    
   }
   

  }
  Move();


 }
 private void Move()//移動(dòng)
 {


  if (Vector3.Distance(bodyList[0].position, map[(int)mapAddress[0].x + x, (int)mapAddress[0].y + z]) < 0.7f)//當(dāng)前坐標(biāo)和目標(biāo)位置的距離小于0.5f
  {
   
   for (int i = mapAddress.Count - 1; i > 0; i--)//刷新后一個(gè)的目標(biāo)為前一個(gè)的目標(biāo)
   {
    mapAddress[i] = mapAddress[i-1];
   }
   
   mapAddress[0] = new Vector2(mapAddress[0].x + x, mapAddress[0].y + z);//刷新第一個(gè)目標(biāo)的位置

  }
  else
  {
   for (int i = bodyList.Count - 1; i > 0; i--)//移動(dòng)
   {
    bodyList[i].position = Vector3.MoveTowards(bodyList[i].position,
              map[(int)mapAddress[i - 1].x, (int)mapAddress[i - 1].y], Time.deltaTime * speed);

   }
   bodyList[0].position = Vector3.MoveTowards(transform.position,
             map[(int)mapAddress[0].x + x, (int)mapAddress[0].y + z], Time.deltaTime * speed);
  }
 }

 private void OnCollisionEnter(Collision collision)//碰撞到了食物就增加身體長(zhǎng)度
 {

  if (collision.collider.tag == "Food")
  {
   
   Destroy(collision.collider.gameObject);
   GameObject tmpGameObject=new GameObject();
   Vector2 tmpVec = new Vector2();
   
   tmpGameObject = Instantiate(bodyObj, map[(int)mapAddress[mapAddress.Count - 1].x+x, (int)mapAddress[mapAddress.Count - 1].y +z], Quaternion.identity);//生成body物體
   tmpVec = new Vector2(mapAddress[mapAddress.Count - 1].x+x, mapAddress[mapAddress.Count - 1].y +z);
   //增加身體的transform和目標(biāo)向量
   bodyList.Add(tmpGameObject.transform);
   mapAddress.Add(tmpVec);
  }
  
 }
}

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

java經(jīng)典小游戲匯總

javascript經(jīng)典小游戲匯總

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

徐水县| 天台县| 和政县| 鄂托克旗| 西贡区| 斗六市| 昭平县| 石阡县| 色达县| 新晃| 浏阳市| 衢州市| 读书| 巢湖市| 远安县| 镇远县| 德化县| 元谋县| 武宣县| 增城市| 鄱阳县| 岳普湖县| 桂平市| 阳朔县| 剑阁县| 都江堰市| 盖州市| 锡林郭勒盟| 鹰潭市| 维西| 乌鲁木齐县| 太康县| 松原市| 广宗县| 南宁市| 德化县| 铜山县| 佛学| 黎城县| 紫阳县| 石景山区|