小球是这个游戏的核心,需要添加刚体
添加刚体后我们可以创建小球的移动代码,用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);
}
}
由于道具都很简单,接下来基本上只放代码了
当小球碰到挂载代码的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);
}
}
}