Unity入门操作_线性,球形检测_014
2017-08-21 20:12
281 查看
线性操作:伤害门。
生成游戏对象
using UnityEngine;
using System.Collections;
public class CreateCubes : MonoBehaviour {
}
对象移动
using UnityEngine;
using System.Collections;
public class MoveDoor : MonoBehaviour {
}
判断是否受到伤害
using UnityEngine;
using System.Collections;
public class PhysicsDemo : MonoBehaviour {
}
球形检测:吸铁石效果。
玩家移动
using UnityEngine;
using System.Collections;
public class CoinMove : MonoBehaviour {
}
金币移动和消失的条件
using UnityEngine;
using System.Collections;
public class CubeMove : MonoBehaviour {
}
生成游戏对象
using UnityEngine;
using System.Collections;
public class CreateCubes : MonoBehaviour {
// Use this for initialization void Start () { } float timer = 0.0f; // Update is called once per frame void 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 initialization void Start () { } // Update is called once per frame void 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 initialization void Start () { } // Update is called once per frame void 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 initialization void Start () { } // Update is called once per frame void 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 initialization void Start () { } // Update is called once per frame void 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; } }
}
相关文章推荐
- Unity15--射线伤害门程序:(线性检测)和吃金币,吸铁石功能的实现:(球形检测)
- Unity入门操作_生成有序图案_008
- Unity入门操作_UGUI_InputFie_026
- Unity入门操作_动画系统_Mecanim_032
- Unity入门操作_积木_009
- Unity入门操作_动画系统_Legacy_031
- Unity 碰撞检测 OnTriggerEnter 入门
- 【笨木头Unity】入门之旅009:Demo之四处找死(四)_在代码里操作物体
- 网络:Unity3d检测当前网络环境wifi/3G/4G,以及对服务器的Ping操作
- Unity入门操作_关节,布料_017
- Unity入门操作_协程_023
- Unity入门操作_UGUI_ScrollBar_029
- Unity入门操作_向量_002
- Unity入门操作_PlayerPrefs存储_037
- Unity入门操作_ 规律移动_007
- Unity入门操作_粒子系统_034
- Unity入门操作_Transform组件_003-01
- Unity入门操作_鼠标寻路 _020
- Unity入门操作_UGUI_事件_030
- Unity入门操作_ 角色控制器_015