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);
        }        
    }
相关推荐
白宇横流学长18 小时前
基于UE引擎的格斗类游戏《SE2》的开发与实现
游戏
风酥糖18 小时前
Godot游戏练习01-第16节-游戏中的状态机
算法·游戏·godot
今夕资源网20 小时前
开源轻量硬件监控软件LiteMonitor超小体积极致轻盈,可实时监测CPU、GPU、内存、磁盘、网络等系统性能,办公游戏皆适配,一键掌控电脑硬件状态
游戏·系统监控·软件·轻量·系统性能·硬件监控软件·硬件监控
guygg881 天前
基于STM32的贪吃蛇游戏实现(OLED屏)
stm32·嵌入式硬件·游戏
CDN3601 天前
360CDN SDK 游戏盾:轻量化接入 + 强防护实测
运维·游戏·网络安全
2501_918126912 天前
学习所有6502写游戏存档的语句
汇编·嵌入式硬件·学习·游戏·个人开发
云边散步2 天前
godot2D游戏教程系列二(18)
笔记·学习·游戏
Swift社区2 天前
Claw 游戏背后的历史
游戏
Candy 9172 天前
英语单词五子棋游戏
游戏·英语单词五子棋游戏
2501_918126912 天前
学习所有6502写游戏地图的语句
汇编·嵌入式硬件·学习·游戏·个人开发