上海专业网站建设信息,网站开发 报价,建网站首页图片哪里找,图书馆网站建设公司//1.添加地面
1#xff09;创建一个平面#xff0c;命名为Ground。 2)创建一个Materials文件夹#xff0c;并在其中创建一个Ground材质#xff0c;左键拖动其赋给平面Plane。 3)根据喜好设置Ground材质和Ground平面的属性。 // 2.创建墙体
1#xff09;创建一个Cube创建一个平面命名为Ground。 2)创建一个Materials文件夹并在其中创建一个Ground材质左键拖动其赋给平面Plane。 3)根据喜好设置Ground材质和Ground平面的属性。 // 2.创建墙体
1创建一个Cube命名为Brick并拖曳至设置的Prefab文件夹作为预制体。 2在Materials文件夹中创建一个Brick材质操作同1.2)1.3)
3网格与捕捉设置 设置好后可利用Ctrl加鼠标拖动步移物体。或点亮带磁铁的标记直接移动物体。 // 设置预制体后的Brick 4全选一直按住Shift点击Brick到Brick10 或直接在图形界面选中物体,CtrlD复制粘贴向上拖动。 5创建一个空物体对象作为上一层文件夹父类。 // 3.控制游戏物体左右移动
1创建一个Movement脚本控制相机的移动 2创建一个Script文件夹放置脚本文件 // Movement脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Movement : MonoBehaviour
{public int speed 1; // 定义一个属性用于控制速度// Start is called before the first frame updatevoid Start(){// transform.Translate(Vector3.up); // 开始游戏时控制相机向上移动1m}// Update is called once per framevoid Update() // 每秒执行的频率不固定{float h Input.GetAxis(Horizontal); // h:通过按下A、D键来控制移动float v Input.GetAxis(Vertical); // V:通过按下W、S键来控制移动transform.Translate(new Vector3(h, v, 0) * speed * Time.deltaTime); // h:控制水平方向x轴的移动;v:控制上下方向y轴的移动// 默认hv为一帧一米Time.deltaTime为一帧的秒数speed可看作为速度的倍数——每秒的速度*倍数// 检测帧率// Debug.Log(Time.deltaTime); // 获取上一帧的时间一帧的秒速// Debug.Log(1 / Time.deltaTime); // FPS frame per second,每秒执行的帧数}//private void FixedUpdate() // 每秒执行的频率固定//{// Debug.Log(Time.deltaTime); // 获取上一帧的时间一帧的秒速// Debug.Log(1 / Time.deltaTime); // FPS frame per second,每秒执行的帧数//}
}// 4.创建小球
1建立一个Sphere命名为Bullet并鼠标拖曳至Prefab文件夹作为预制体删除Bullet实体。 2建立子弹脚本 // 定义一个游戏物体子弹预制体
public GameObject bulletPrefab; // 定义一个游戏物体子弹预制体
// 通过鼠标拖曳预制体Bullet指定对象 5.判断鼠标按下并创建子弹
// 脚本 void Update(){// iftrue——运行{代码}否则跳过if (Input.GetMouseButtonDown(0)) // 在用户按下给定鼠标按钮的那一帧内返回true。0 ——鼠标左键{GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation);}}
// 给预设体Bullet设置刚体组件和创建脚本一样 // 运行 6.发射子弹
// 方案一使用AddForce方法施加力 void Update(){// iftrue——运行{代码}否则跳过if (Input.GetMouseButtonDown(0)) // 在用户按下给定鼠标按钮的那一帧内返回true。0 ——鼠标左键{GameObject bullet GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation); // 设置一个bullet接受克隆的一个游戏物体Rigidbody rd bullet.GetComponentRigidbody(); // 得到bullet身上的刚体组件// 施加力的方案// 方案一不方便观察速度rd.AddForce(Vector3.forward * 80); // 默认施加一个向前的1N的力 * 80}} // 方案二直接给一个速度
rd.velocity Vector3.forward * 35; // 直接给一个速度 7.墙壁物理模拟
// 给预设体Brick添加刚体 // 全代码参考
// Movement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Movement : MonoBehaviour
{public int speed 1; // 定义一个属性用于控制速度// Start is called before the first frame updatevoid Start(){// transform.Translate(Vector3.up); // 开始游戏时控制相机向上移动1m}// Update is called once per framevoid Update() // 每秒执行的频率不固定{float h Input.GetAxis(Horizontal); // h:通过按下A、D键来控制移动float v Input.GetAxis(Vertical); // V:通过按下W、S键来控制移动transform.Translate(new Vector3(h, v, 0) * speed * Time.deltaTime); // h:控制水平方向x轴的移动;v:控制上下方向y轴的移动// 默认hv为一帧一米Time.deltaTime为一帧的秒数speed可看作为速度的倍数——每秒的速度*倍数// 检测帧率// Debug.Log(Time.deltaTime); // 获取上一帧的时间一帧的秒速// Debug.Log(1 / Time.deltaTime); // FPS frame per second,每秒执行的帧数}//private void FixedUpdate() // 每秒执行的频率固定//{// Debug.Log(Time.deltaTime); // 获取上一帧的时间一帧的秒速// Debug.Log(1 / Time.deltaTime); // FPS frame per second,每秒执行的帧数//}
} // Shoot
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Shoot : MonoBehaviour
{public GameObject bulletPrefab; // 定义一个游戏物体子弹预制体// Start is called before the first frame updatevoid Start(){// 根据prefab创建实例实例化Prefab克隆物体// GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation);}// Update is called once per framevoid Update(){// iftrue——运行{代码}否则跳过if (Input.GetMouseButtonDown(0)) // 在用户按下给定鼠标按钮的那一帧内返回true。0 ——鼠标左键{GameObject bullet GameObject.Instantiate(bulletPrefab, transform.position, transform.rotation); // 设置一个bullet接受克隆的一个游戏物体Rigidbody rd bullet.GetComponentRigidbody(); // 得到bullet身上的刚体组件// 施加力的方案// 方案一不方便观察速度//rd.AddForce(Vector3.forward * 80); // 默认施加一个向前的1N的力 * 80// 方案二rd.velocity Vector3.forward * 35; // 直接给一个速度}}
}