您的位置:首页 > 移动开发 > Unity3D

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

2017-08-21 20:12 281 查看
线性操作:伤害门。

生成游戏对象

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;
}
}


}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  对象 游戏 unity