Unity3D RunningBall

Running Ball

Unity3D RunningBall_第1张图片控制小球在Z方向的运动

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

public class Player : MonoBehaviour
{

    public float speed;
    public float turnSpeed=10;

    // Start is called before the first frame update
    void Start()
    {
        
    }
    
    // Update is called once per frame
    void Update()
    {
            float x=Input.GetAxis("Horizontal")

       // transform.Translate(0,0,speed*Time.deltaTime);//控制小球在Z方向的运动
            transform.Translate(x*turnSpeed*Time.deltaTime,0,speed*Time.deltaTime);

    }

}

为了使得摄像机跟随小球运动,把摄像机拖进Player对象,使得摄像机成为小球的子物体,让小球运动带动摄像机。

然后将障碍物设置为预制体,给小球添加刚体组件,选中player 的rigidbody的Is kinematic,选中Barrier的BoxCollider 的Is Trigger,并添加脚本

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

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

    private void OnTriggerEnter(Collider other)
    {
    	Debug.Log(other.name+"碰到了我");
    	Time.timeScale=0;
    }
}


为表示小球越过了平台道路,就下落,在Player脚本中添加如下

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.R))
        {
        	SceneManager.LoadScene(0);//按下R键,载入默认的第一个场景,即游戏重新开始
        	Time.timeScale=1;
        	return;
        }
        //下面控制左右运动
        float x=Input.GetAxis("Horizontal");

        transform.Translate(x*turnSpeed*Time.deltaTime,0,speed*Time.deltaTime);

        if(transform.position.x<-4 ||transform.position.x>4)
        {
        	Debug.Log("Player 出界了");
            transform.Translate(0,-10*Time.deltaTime,0);
        }  
        if(transform.position.y<-10 )//下落超过10,就暂停游戏
    	{
    		Time.timeScale=0;
    	}
   }

游戏结束的Ui

添加新的panel,为panel添加text,“恭喜通过关卡”
Unity3D RunningBall_第2张图片

新建一个cube,命名为Finish,建立一个透明的空间,取消Mesh Renderer

挂载脚本Finish.cs

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

public class Finish : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameObject canvas=GameObject.Find("Canvas");
        canvas.transform.Find("Panel").gameObject.SetActive(false);//初始化Finish对象为不激活
    }

    // Update is called once per frame
    private void OnTriggerEnter(Collider other)
    {
        GameObject canvas=GameObject.Find("Canvas");
        canvas.transform.Find("Panel").gameObject.SetActive(true);//被碰撞后激活Finish对象
        Time.timeScale=0;
    }
}

为了保证BgMusic在每次游戏开始是接着上一次播放
创建一个空对象,为他添加AudioSource,加入你喜欢的BgMusic,将它设置为预制对象,从SampleScene里删掉。在Player.cs里添加如下

    public GameObject prefabMusic;
    void Start()
    {
        var music=GameObject.Find("BgMusic");
        if(music==null)
        {
        	var m=Instantiate(prefabMusic,null);
        	m.name="BgMusic";
        	DontDestroyOnLoad(m);
        }
    }

然后将预制体拖进prefabMusic初始化框框里

屏幕旋转

        //屏幕动态旋转效果
        var c=Camers.main.transform;
        Quaternion cur=c.rotation;
        Quaternion target =cur*Quaternion.Euler(0,0,x*1.5f);
        Camers.main.transform.rotation=Quaternion.Slerp(cur,target.0.5f);

设置电梯

新建一个空对象命名为Lift,为其设置多个子对象,第一个子对象设为透明,并设为触发器,在Lift挂载脚本Lift.cs

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

public class Lift : MonoBehaviour
{
	bool lift =false;
    // Start is called before the first frame update


    // Update is called once per frame
    private void Update()
    {
        if(! lift) return;
        transform.parent.Translate(0,10*Time.deltaTime,0);
    }

    private void OnTriggerEnter(Collider other)
    {
    	lift=true;
    }

}

(确保每个子对象都受该脚本控制,检查子对象是否挂载Lift.cs)
player一旦触发到第一个子对象,其他对象就一起抬升

你可能感兴趣的:(趣味应用)