unity新手小球走迷宫小游戏

基本场景

在这里插入图片描述
物体的贴图在asset store里可以下载
在这里插入图片描述

基本物体

小球

小球是这个游戏的核心,需要添加刚体
在这里插入图片描述
添加刚体后我们可以创建小球的移动代码,用addforce给小球力来控制小球的移动

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

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

    // Update is called once per frame
    void Update()
    {
        r.AddForce(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) * 5);
        
    }
}

摄像机

在游戏时我们希望摄像机能跟随小球的移动,于是有如下代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour
{
    public Transform player;
    Transform trans;
    public Vector3 dis;
    // Start is called before the first frame update
    void Start()
    {
        trans = this.transform;
    }

    // Update is called once per frame
    void Update()
    {
        trans.position = player.position + dis;
        trans.LookAt(player);
    }
}

把脚本挂到camera上,dis可以在检视界面直接改
在这里插入图片描述

各种道具

由于道具都很简单,接下来基本上只放代码了

传送板

当小球碰到挂载代码的Quad时传送到指定位置

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

public class RTeleport : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnCollisionEnter(Collision collision)
    {
        Collider collider = collision.collider;
        if (collider.CompareTag("Player"))
            {
            collider.transform.position = new Vector3(23, 0.1f, -15);//位置任意更改
        }
    }
}

跳板

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

public class JumpBoard : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Player"))
        {
            other.GetComponent<Rigidbody>().AddForce(Vector3.up * 300);//给小球一个向上的力
        }
    }
}

旋转障碍物

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

public class Rotate : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

射线检测门

小球通过门时出现一个cube和一个quad,把cube推到quad触发lock代码

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

public class Ray1 : MonoBehaviour
{
    public GameObject Key;//cube
    public GameObject Lock;//quad
    public GameObject Door;
    // Start is called before the first frame update
    void Start()
    {
        
        
    }
    // Update is called once per frame
    void Update()
    {
        Ray ray = new Ray(transform.position , Vector3.right);
        RaycastHit hit;      
        bool res = Physics.Raycast(ray, out hit,7);
        if (res == true)
        {
            Destroy(Door);
            Lock.transform.position = new Vector3(18, 0.1f, 6);
            Key.transform.position = new Vector3(9, 0.5f, 6);
        }     
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lock1 : MonoBehaviour
{
    public GameObject door;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
      
    }
    public void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("key"))
        {
            Destroy(door);
        }
    }
}

终点

由于不会别的,就让小球碰到终点时起飞,芜湖~

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

public class Finish : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnCollisionEnter(Collision collision)
    {
        Collider collider = collision.collider;
        if (collider.CompareTag("Player"))
        {
            collider.GetComponent<Rigidbody>().AddForce(Vector3.up * 5000);
        }
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值