Unity3D-基础3___疯狂教室案例开放

Unity3D-基础3___疯狂教室案例开放

课程要点上:

  1. 模型旋转实现开门效果

  2. 触发器实现开关

  3. 模型旋转实现开门效果

a. 模型中心点
模型身上的坐标轴的中心点,也就是我们模型的中心点。
模型的的位置,旋转,缩放都是相对于模型的中心点来进行变化的

  1. 改变模型中心点

创建一个空物体,创建父子关系,通过父物体来控制子物体。
也就间接的改变了模型的中心点。

  1. 中心点工具

Center:当选中两个模型的时候,设置为“Center”,模型组的中心点就在
两个模型的中间中心位置。
Pivot:当选中两个模型的时候,设置为“Pivot”,模型组的中心点就在后选
中的模型的中心点位置。


2.触发器实现开关门

  1. 添加触发器

① 创建一个空物体,添加“Box Collider”组件,并设置大小和中心点;
② 将“Box Collider”勾选“Is Trigger”变成触发器;

  1. 代码实现触发器开关门

OnTriggerEnter()
OnTriggerExit()

  1. 查找游戏物体

GameObject.Find(string):[静态方法] 通过名字查找游戏物体
String 游戏物体的名字
碰撞体代码

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

public class Door : MonoBehaviour
{
    private Transform _transform;

    // Use this for initialization
    void Start()
    {
        _transform = gameObject.GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
//        if (Input.GetKeyDown(KeyCode.Z))
//        {
			open
//            OpenDoor();
//        }
//
//        if (Input.GetKeyUp(KeyCode.Z))
//        {
		close
//            CloseDoor();
//        }
    }

    public void OpenDoor()
    {
        _transform.Rotate(Vector3.up, 90);
    }

    public void CloseDoor()
    {
        _transform.Rotate(Vector3.up, -90);
    }
}

触发体代码:

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

public class Doortrigger : MonoBehaviour
{
	private Door _door;
	// Use this for initialization
	void Start ()
	{
		_door=GameObject.Find("door").GetComponent<Door>(); //找到door对象模型的Door脚本组件
	}
	
	// Update is called once per frame
	void Update () { 
	
	}

	private void OnTriggerEnter(Collider other)
	{
		Debug.Log("enter");
		if (other.gameObject.name=="Student")
		{
			//open
			_door.OpenDoor();
		}
	}

	private void OnTriggerExit(Collider other)
	{Debug.Log("leave");
		if (other.gameObject.name=="Student")
		{
			//close
			_door.CloseDoor();
		}
	}
}
  

人物代码

using UnityEngine;
using System.Collections;

public class RgidibodyMove : MonoBehaviour {

	private Rigidbody m_Rigidboby;
	private Transform m_Transform;

	void Start () {
		m_Rigidboby = gameObject.GetComponent<Rigidbody> ();
		m_Transform = gameObject.GetComponent<Transform> ();
	}

	void Update () {
		if(Input.GetKey(KeyCode.W))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.forward * 0.2f);
		}

		if(Input.GetKey(KeyCode.S))
		{	
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.back * 0.2f);
		}

		if(Input.GetKey(KeyCode.A))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.left * 0.2f);
		}

		if(Input.GetKey(KeyCode.D))
		{
			m_Rigidboby.MovePosition (m_Transform.position + Vector3.right * 0.2f);
		}
	}

//	private void OnCollisionEnter(Collision other)
//	{
//		if (other.gameObject.name != "Ground")
//		{
//			Debug.Log("enter"+other.gameObject.name);
//		}
//	}
//
//	private void OnCollisionExit(Collision other)
//	{
//		if (other.gameObject.name != "Ground")
//		{
//			Debug.Log("exit"+other.gameObject.name);
//		} 
//
//	}
//	private void OnCollisionStay(Collision other)
//	{
//		if (other.gameObject.name != "Ground")
//		{
//			Debug.Log("stay"+other.gameObject.name);
//		}
//	}
	private void OnTriggerEnter (Collider other)
	{
		Debug.Log("student enter"+other.gameObject.name);
	}

	private void OnTriggerStay(Collider other)
	{
		Debug.Log("student stay"+other.gameObject.name);
	}

	private void OnTriggerExit(Collider other)
	{
		Debug.Log("student exit"+other.gameObject.name);
	}	
}



课程要点下:

通过Tag(标签查找物体)
给模型添加tag标签
在inspector


3.通过 Tag 标签查找 N 个物体
GameObject.FindGameObjectsWithTag(string):[静态方法]
通过特定的标签,查找到所有“贴有”该标签的游戏物体,返回一个数组。
String:标签名
4.for 循环输出模型信息
通过 for 循环遍历 FindGameObjectsWithTag()方法返回的数组,输出
游戏物体的信息。
代码:

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

public class DeskTrigger : MonoBehaviour {
	private GameObject[] _gameObjects;
	
	// Use this for initialization
	void Start () {
		_gameObjects=GameObject.FindGameObjectsWithTag("Desks");
//		for (int i = 0; i < _gameObjects.Length; i++)
//		{
//			Debug.Log(_gameObjects[i].name);
//		}
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.Z))
		{
			//up
			DeskUP();
		}

		if (Input.GetKeyUp(KeyCode.Z))
		{
			//down
			DeskDOWN();
		}
	}

	void DeskUP()
	{
		for (int i = 0; i < _gameObjects.Length; i++)
		{
			_gameObjects[i].GetComponent<Transform>().Translate(Vector3.up*2,Space.Self);
		}
	}

	void DeskDOWN()
	{
		for (int i = 0; i < _gameObjects.Length; i++)
		{
			_gameObjects[i].GetComponent<Transform>().Translate(Vector3.down*2,Space.Self);
		}
	}

	private void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.name=="Student")
		{
			DeskUP();
		}
	}

	private void OnTriggerExit(Collider other)
	{
		if (other.gameObject.name=="Student")
		{
			DeskDOWN();
		}
	}
}

代码


游戏的打包与发布

1.游戏打包发布简介
1.简介
现在的项目文件必须在 Unity 引擎中才能运行,通过“打包发布”可以将工程
文件转换成独立的“游戏文件”,就可以脱离 Unity 引擎直接在电脑上运行。
打包好的“游戏文件”就可以到处发布传播了。
2.Unity 发布游戏
Unity 的最大的一个特点就是“跨平台运行”,一处开发多处运行。
常用的发布平台:Windows,Android,IOS,Mac,Web…
我们现在演示打包发布 Windows 版本的游戏。
2.Unity 发布 PC 版游戏
1.Build Settings[生成设置]
File–>Build Settings 弹出项目生成设置面板。
① 选择要发布到的平台;
② 添加要发布的场景;
2.Player Settings[详细设置]
Company Name:公司名称
Product Name:产品名称(游戏名称)
Default Icon:默认图标
3.成品文件介绍
一个 exe 可执行文件,一个 Data 数据文件夹,两个缺一不可以,且不可分割。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值