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);
        }        
    }
相关推荐
电报号dapp1192 小时前
链游新纪元——链游平台开发引领游戏新潮流!
游戏·web3·去中心化·区块链
cver1237 小时前
CSGO 训练数据集介绍-2,427 张图片 AI 游戏助手 游戏数据分析
人工智能·深度学习·yolo·目标检测·游戏·计算机视觉
witton7 小时前
Go语言网络游戏服务器模块化编程
服务器·开发语言·游戏·golang·origin·模块化·耦合
不绝1919 小时前
怪物机制分析(有限状态机、编辑器可视化、巡逻机制)
网络·游戏·unity·游戏引擎
深海潜水员1 天前
【Behavior Tree】-- 行为树AI逻辑实现- Unity 游戏引擎实现
游戏·unity·c#
群联云防护小杜1 天前
构建分布式高防架构实现业务零中断
前端·网络·分布式·tcp/ip·安全·游戏·架构
Thomas_YXQ1 天前
Unity3D游戏内存优化指南
游戏·unity·职场和发展·性能优化·蓝桥杯·游戏引擎·unity3d
Luna-player2 天前
黑布淡入淡出效果
经验分享·游戏
TESmart碲视3 天前
HKS201-M24 大师版 8K60Hz USB 3.0 适用于 2 台 PC 1台显示器 无缝切换 KVM 切换器
单片机·嵌入式硬件·物联网·游戏·计算机外设·电脑·智能硬件
翻滚吧键盘3 天前
查看linux中steam游戏的兼容性
linux·运维·游戏