Unity小球吃方块小游戏

本文介绍了如何在Unity中创建一个简单的游戏场景,包括制作可食用的食物预制体、移动小球以吃掉食物、以及给摄像头添加跟随小球的脚本。通过这些步骤,读者将学习到基本的游戏对象交互和控制技术。
摘要由CSDN通过智能技术生成

1.创建一个简单的场景。

2.制作食物并制作成预制体。给食物打上food的标签,用脚本让它被吃掉后消失。

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

public class szsz : MonoBehaviour
{
    public float zxcspeed;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(Vector3.up, zxcspeed);
    }
}

 

3.创建一个可以操控的小球。并用脚本控制可以吃掉食物

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

public class move : MonoBehaviour
{
    public Rigidbody rg;
    // Start is called before the first frame update
    void Start()
    {
        rg = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        rg.AddForce(new Vector3(h, 0, v));

    }


    //}
    private void OnTriggerEnter(Collider other)//触发器的方法
    {
        if (other.gameObject.tag == "food")
        {
            Destroy(other.gameObject);
        }
    }

}

4.给摄像头一个可以跟随小球的脚本。

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

public class szxc : MonoBehaviour
{
    public GameObject player;
    public Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - player.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = player.transform.position + offset;
    }
}

5.这样就可以玩了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值