unity案例学习之“梦幻世界”

1.球体的移动和碰撞检测

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Playshop : MonoBehaviour
{
    private Rigidbody shopt;
    public GameObject txt_out;
    public float speet = 8;
    public Text txt;
    //private Rigidbody y;
    private int score = 0;
    //private Rigidbody y;
    //public GameObject shopt;//拖拽获取组件
    // Start is called before the first frame update
    void Start()
    {
         shopt = GetComponent<Rigidbody>();
       


    }

    // Update is called once per frame
    void Update()
    {
        //1
        //GameObject y = GameObject.FindWithTag("play");//获取游戏物体
        //Rigidbody c = y.GetComponent<Rigidbody>();//获取刚体组件
        //2
        //Rigidbody y = GetComponent<Rigidbody>();//获取当前游戏物体的刚体组件
        //3
        //y = shopt.GetComponent<Rigidbody>();//获取拖拽游戏物体的刚体组件

       
        //移动
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        shopt.AddForce(new Vector3(h, 0, v)*speet);//刚体移动函数AddForce()

    }
    //碰撞检测
    //is Trigger可以关闭或默认
    void OnCollisionEnter(Collision collision)
    {
       
        //发生实际的物理效果
        //string name = collision.collider.name;
        //collision.collider获取游戏物体cube身上的collider组件
        //print(name);
        if (collision.collider.tag == "picup") {
            Destroy(collision.collider.gameObject);//销毁方法
        };
    }


    //触碰检测
    //is Trigger要开启
     void OnTriggerEnter(Collider collider)
    {
        Debug.Log(123);
        if (collider.tag == "picup")
        {
            score++;
            txt.text = score.ToString();//将number类型转为字符串类型

            if (score == 19) {
                txt_out.SetActive(true);//显示与隐藏
            }
            Destroy(collider.gameObject);//销毁
        };
    }

    

}

2.方块的自动旋转

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

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

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(0,1,0));//围绕自身Y轴旋转
    }
}

3.相机的跟随

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Play_camera : MonoBehaviour
{
    public Transform playerTransform;

    public Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {
        //相机的位置
        offset = transform.position - playerTransform.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = playerTransform.position + offset;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值