unity新手小球走迷宫小游戏

目录

  • 基本场景
  • 基本物体
    • 小球
    • 摄像机
  • 各种道具
    • 传送板
    • 跳板
    • 旋转障碍物
    • 射线检测门
    • 终点

基本场景

unity新手小球走迷宫小游戏_第1张图片
物体的贴图在asset store里可以下载
unity新手小球走迷宫小游戏_第2张图片

基本物体

小球

小球是这个游戏的核心,需要添加刚体
unity新手小球走迷宫小游戏_第3张图片
添加刚体后我们可以创建小球的移动代码,用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可以在检视界面直接改
unity新手小球走迷宫小游戏_第4张图片

各种道具

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

传送板

当小球碰到挂载代码的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);
        }
    }
}

你可能感兴趣的:(unity3d)