iT邦幫忙

2025 iThome 鐵人賽

0

這一篇我們要讓貪吃蛇吃掉一顆蘋果後,身體變長一節
新增身體部分
在hieracky中新增一個circle,並改變它的顏色,接著把他拉到底下project的部分
https://ithelp.ithome.com.tw/upload/images/20251020/20178853lKsdb2KZQF.png
在snack的腳本中新增這一行:public Transform bodyPrefab;
完整程式碼:

using UnityEngine;

public class Snack : MonoBehaviour
{
    Vector3 direction;
    public float speed;
    public Transform bodyPrefab;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        Debug.Log(transform.position);

        Time.timeScale = speed;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("W");
            direction = Vector3.up;
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("A");
            direction = Vector3.left;
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("S");
            direction = Vector3.down;
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("D");
            direction = Vector3.right;
        }


    }
    void FixedUpdate()
    {
        transform.Translate(direction);

    }
}

儲存好後,unity的頁面會多一個body prefab變數,再將body拖曳進去
https://ithelp.ithome.com.tw/upload/images/20251020/20178853KczAOhZ8CF.png
打開apple,inspector介面有一個tag選項,打開後按add tag,接著按+,輸入Food,出去後再按tag,選擇剛剛新增的Food
https://ithelp.ithome.com.tw/upload/images/20251020/20178853cZjbatRgV1.png
https://ithelp.ithome.com.tw/upload/images/20251020/20178853RqqFq5s0Rg.png
最後,在Snack的腳本裡輸入這串完整程式碼:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class Snack : MonoBehaviour
{
    Vector3 direction;
    public float speed;
    public Transform bodyPrefab;

    public List<Transform> bodies = new List<Transform>();
    void Start()
    {

        Time.timeScale = speed;
        bodies.Add(transform);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("W");
            direction = Vector3.up;
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("A");
            direction = Vector3.left;
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("S");
            direction = Vector3.down;
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("D");
            direction = Vector3.right;
        }


    }
    private void FixedUpdate()
    {
        for(int i = bodies.Count - 1; i > 0; i--)
        {
            bodies[i].position = bodies[i - 1].position;
        }
        transform.Translate(direction);

    }
    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.CompareTag("Food"))
        {

            bodies.Add(Instantiate(bodyPrefab
            , transform.position
            , Quaternion.identity));

        }
    }
}


上一篇
DAY14-讓貪吃蛇吃東西吧!(2)
下一篇
DAY16-如何讓遊戲結束(1)
系列文
從零開始做出我的第一款 Unity 遊戲21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言