Unity3D笔记三 物理引擎
一、物理引擎
1、物理引擎就是模拟真实世界中物体碰撞、跌落等反应,通过Ballance、愤怒的小鸟来理解什么是物理引擎。Unity的物理引擎使用的是NviDIA的PhysX。
2、选中一个游戏对象,主菜单->Component->Physics->Rigidbody,这样就添加了刚体组件。一旦给一个GameObject添加刚体组件,它就会受重力、碰撞等的反应、无法进入等。地面用Plane。加光照效果会更好。
3、Rigidbody组件的属性:Mass:质量,一般不用大于10;Drag:摩擦力。Use Gravity、Freezze Position、Freeze Rotation等。
4、材质:可以设定刚体是橡胶的、木头的、冰。Import Packages->Physic Materials,修改**Collider的Material指向相应材料即可。
1、
2、
3、
4、
4、添加刚体组件
远离屏幕 负值
二、给'力'
1、可以给游戏对象一个力,这样对象可以响应,比如愤怒的小鸟。注意添加了刚体组件才能给力,否则rigidbody为null。
2、rigidbody.AddForce(Vector3.up*10,ForceMode.Impulse);给一个向上为10的力,Impulse表示冲击力。点击鼠标给小球一个向上的力。
3、rigidbody.AddForce(new Vector3(3,3,0),ForceMode.Impulse);给一个x为3,y为3,抛物线出去。
4、实现向鼠标点击的地方发射球。
1)屏幕(鼠标)坐标转向世界标;
Camera.mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,3));//表示z方向的假定深度,因为屏幕是二维没有深度,越大越靠里
2)实现见备注:vector2-vector1表示从vector1指向vector2的向量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using UnityEngine; using System.Collections; public class Add : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { Vector3 ve = Camera.main.ScreenToWorldPoint( new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3)); //屏幕坐标->世界坐标 目标点的坐标 z轴3个单位 Vector3 dir = ve - Camera.main.transform.position; //目标点-摄像机点的位置 this .gameObject.rigidbody.AddForce(dir * 2, ForceMode.Impulse); } } } |
三、推箱子
屏幕坐标 世界坐标 用摄像头
动态创建游戏对象
1、使用CreatePrimitive方法创建对象,创建出的对象不需要Add之类的就可以显示出来:
GameObject cube=GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position=new Vector3(0,0,0);
cube.AddComponent(typeof(Rigidbody));
cube.name="";//设定名字
cube.renderer.material.color=Color.red;
玩:点击鼠标就创建一个Cube,连续点击
调用AddComponent方法来动态为GameObject增加组件(脚本、RigidBody等所有Component菜单下的)
2、动态创建出来的对象运行时在Hierarchy中可以看到,可以帮助检查内存泄露。
3、Destroy(obj)是立即销毁游戏对象,比如被打死后消失:测试,点击鼠标右键销毁。注意:
Destory(this.gameObject);
4、实现炮弹发射和生成4*4个箱子的功能,及时销毁不用的箱子和子弹:OnBecameInvisible事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using UnityEngine; using System.Collections; public class Add : MonoBehaviour { // Use this for initialization void Start () { //初始化对象 //GameObject gameObject =GameObject.CreatePrimitive(PrimitiveType.Cube);//创建一个正方体 //gameObject.transform.position = new Vector3(0, 3, -4); } // Update is called once per frame void Update () { #region 世界坐标 //if (Input.GetMouseButtonDown(0)) //{ // Vector3 ve = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3));//鼠标点击的地方 屏幕坐标->世界坐标 目标点的坐标 z轴3个单位 // Vector3 dir = ve - Camera.main.transform.position;//目标点-摄像机点的位置 // this.gameObject.rigidbody.AddForce(dir * 2, ForceMode.Impulse); //} #endregion #region 单击鼠标创建对象 //if (Input.GetMouseButtonDown(0)) //{ // GameObject gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube); // gameObject.transform.position = new Vector3(0, 0, 0); // gameObject.AddComponent<Rigidbody>(); //} #endregion #region 销毁对象 //if (Input.GetMouseButtonDown(0)) //{ // var sphere =GameObject.Find("Sphere"); // Destroy(sphere, 2); //} #endregion } } |
打箱子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using UnityEngine; using System.Collections; public class DaPao : MonoBehaviour { // Use this for initialization void Start () { for ( int i = 0; i < 4; i++) { for ( int j = 0; j < 4; j++) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(i, j, -1); cube.AddComponent<Rigidbody>(); //添加刚体 cube.AddComponent<Destroy>(); //Destroy.cs 类中的OnBecameInvisible } } } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere); //创建子弹 ['bʊlɪt] bullet.transform.position = Camera.main.transform.position; bullet.AddComponent<Rigidbody>(); bullet.AddComponent<Destroy>(); Vector3 vector = Camera.main.ScreenToWorldPoint( new Vector3(Input.mousePosition.x,Input.mousePosition.y,3)); bullet.rigidbody.AddForce((vector - Camera.main.transform.position)*8, ForceMode.Impulse); //向量角度太小打不倒箱子,向量的角度增加8 //不增加这个不但打不到箱子,还不能打指定箱子 } } } |
新增一个销毁对象的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using UnityEngine; using System.Collections; public class Destroy : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } //当游戏对象离开摄像头时销毁对象 void OnBecameInvisible() { Destroy( this .gameObject); } //赋给箱子和球 } |
作者:PEPE
出处:http://pepe.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述