Unity入门操作_线性,球形检测_014

来源:互联网 发布:js方法的构造函数 编辑:程序博客网 时间:2024/06/03 22:03

线性操作:伤害门。
生成游戏对象
using UnityEngine;
using System.Collections;
public class CreateCubes : MonoBehaviour {

// Use this for initializationvoid Start () {}float timer = 0.0f;// Update is called once per framevoid Update () {    timer += Time.deltaTime;    if (timer>3)    {        timer -= 3;        GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);        obj.transform.position = new Vector3(0,0.5f,0);        obj.AddComponent<MoveDoor>();    }}

}
对象移动
using UnityEngine;
using System.Collections;
public class MoveDoor : MonoBehaviour {

// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {    gameObject.transform.Translate(gameObject.transform.forward * Time.deltaTime);}

}
判断是否受到伤害
using UnityEngine;
using System.Collections;
public class PhysicsDemo : MonoBehaviour {

public GameObject startObj;public GameObject endObj;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {    RaycastHit hit;    if (Physics.Linecast(startObj.transform.position, endObj.transform.position,out hit))    {        if (!hit.collider.gameObject.CompareTag("Door"))        {            Destroy(hit.collider.gameObject);        }    }}

}
球形检测:吸铁石效果。
玩家移动
using UnityEngine;
using System.Collections;
public class CoinMove : MonoBehaviour {

public Transform target;public bool isMove = false;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {    if (isMove)    {        transform.position = Vector3.Lerp(transform.position, target.position, 0.2f);    }}void OnTriggerEnter(Collider other){    if (other.gameObject.CompareTag("Player"))    {        Destroy(gameObject);    }}

}
金币移动和消失的条件
using UnityEngine;
using System.Collections;
public class CubeMove : MonoBehaviour {

bool isMagnet = false;public float speed;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {    transform.Translate(transform.forward *speed * Time.deltaTime);    if (isMagnet)    {        Collider[] cols = Physics.OverlapSphere(transform.position, 10);        foreach (var item in cols)        {            if (item.gameObject.CompareTag("Coin"))            {                item.GetComponent<CoinMove>().isMove = true;            }        }    } }void OnTriggerEnter(Collider other){    if (other.gameObject.tag == "Magnet")    {        Destroy(other.gameObject);        isMagnet = true;    }}

}
这里写图片描述

这里写图片描述