Roll-A-Ball 游戏

Roll-A-Ball 游戏

1)学习资料

2)核心代码:

功能1:用W A S D控制小球移动的脚本:

新建一个C#脚本叫做sphereControll,添加到 小球身上。

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sphereControll : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            GetComponent<Rigidbody>().AddForce(new Vector3(0, 0, 10));
        }
        if (Input.GetKey(KeyCode.S))
        {
            GetComponent<Rigidbody>().AddForce(new Vector3(0, 0, -10));
        }
        if (Input.GetKey(KeyCode.A))
        {
            GetComponent<Rigidbody>().AddForce(new Vector3(-10, 0, 0));
        }
        if (Input.GetKey(KeyCode.D))
        {
            GetComponent<Rigidbody>().AddForce(new Vector3(10, 0, 0));
        }
        if (Input.GetKey(KeyCode.Space))
        {
            GetComponent<Rigidbody>().AddForce(new Vector3(0, 10, 0));
        }
    }
}

或者:用Input.getAxis控制小球移动的脚本:

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sphereControll : MonoBehaviour
{
    float horizontal;
    float vertical;
    public float speed=10;
    // Update is called once per frame
    void Update()
    {
        horizontal=Input.GetAxis("Horizontal");
        vertical=Input.GetAxis("Vertical");

        GetComponent<Rigidbody>().AddForce(new Vector3(horizontal,0,vertical)*speed);
    }
}

功能2:摄像机跟随脚本,

新建一个C#脚本叫做CameraController.cs,添加到 摄像机身上。

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    public GameObject player;
    Vector3 offset;

    void Start()
    {
        offset = transform.position - player.transform.position;
    }

    void Update()
    {
        transform.position = player.transform.position + offset;
    }
}

功能3:物块自动旋转

新建脚本,Rotator.cs,挂到要旋转的物体上。

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotator : MonoBehaviour
{
    void Update()
    {
        transform.Rotate(new Vector3(15,30,45)*Time.deltaTime);
    }
}

功能4:碰到即消失。

在小球的脚本中,添加以下代码,若碰到了tag是pickup的物体,则销毁该物体

csharp 复制代码
void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "pickup")
        {
            Destroy(other.gameObject);
        }        
    }
相关推荐
FairGuard手游加固8 小时前
手游加固怎么做?SO、IL2CPP、AssetBundle 与运行时保护详解
安全·游戏·unity
Vex2une8 小时前
GRLP游戏运行库合集:一站式解决DLL缺失与启动报错实战指南 | Game Runtime Libraries Package
游戏·vc++运行库·0xc000007b·游戏运行库·dll缺失修复·directx修复·游戏报错解决
tommyjiatong20 小时前
SDK游戏盾的工作原理
游戏·网络安全·sdk盾
tianyatest2 天前
魔兽争霸 III 接入大语言模型:基于文件通道的混合式游戏 AI 智能体系统设计与实现
人工智能·游戏·语言模型·war3
开开心心_Every2 天前
黄金价格软件国内外价格实时监控超方便
运维·服务器·游戏·智能手机·pdf·lua·perl
hflmwl83 天前
Wise Game Booster 游戏加速软件
游戏·wisegamebooster·游戏加速软件
Solange__3 天前
工业产线怎么用AI生成3D场景?从现场采集、部件重建到数字孪生交付的完整方案
人工智能·游戏·3d·ai·语言模型·自然语言处理·关卡设计
一次旅行3 天前
2026.10.13 AI产业深度解读|硬件互联标准更新;Agent工程启示;开源社区AI政策转向;游戏与科研AI双线突破
人工智能·游戏·开源
xiaolang_8616_wjl3 天前
预告_c++游戏
游戏
乌萨达3 天前
绿梦时空之声pc怎么玩?预下载、模拟器安装与键鼠设置指南
游戏·安卓模拟器·雷电模拟器