#include<stdio.h
#include<stdio.h
鼠标事件:
鼠标事件,都是当鼠标和gui或者碰撞体(Collider)交互时候触发。需要说明的是drag其实就是鼠标down后up之前持续每帧都会发送此消息。
OnMouseDown:当鼠标上的按钮被按下时触发的事件;
OnMouseDrag:当用户鼠标拖拽GUI元素或碰撞体时调用;
OnMouseEnter:当鼠标进入物体范围时被调用;
OnMouseExit:当鼠标退出时被调用;
OnMouseOver:当鼠标移动到某对象的上方时触发的事件;
OnMouseUp:当鼠标按键被松开时触发的事件
按下事件:Input.GetMouseButtonDown()
该方法只有一个参数,参数为0时,代表鼠标左键被按下,参数为1的时候,代表鼠标右键被按下,参数为2的时候代表鼠标中键被按下。
void Update() {
if (Input.GetMouseButtonDown(0))
Debug.Log("Pressed left click.");
if (Input.GetMouseButtonDown(1))
Debug.Log("Pressed right click.");
if (Input.GetMouseButtonDown(2))
Debug.Log("Pressed middle click.");
}
抬起事件:Input.GetMouseButtonUp()
鼠标在按下后肯定要抬起,按下的时候会触发按下事件,抬起的时候会触发抬起事件。与按下事件相同,抬起事件也只有一个参数,当参数为0的时候代表鼠标左键抬起,参数为1的时候代表鼠标右键被抬起,参数为2的时候代表鼠标中键被抬起。
void Update() {
if (Input.GetMouseButtonDown(0))
Debug.Log("Pressed left click.");
if (Input.GetMouseButtonDown(1))
Debug.Log("Pressed right click.");
if (Input.GetMouseButtonDown(2))
Debug.Log("Pressed middle click.");
}
长按事件:Input.GetMouseButton()
检测鼠标三个三个按键中某一按键一直按下的状态或者是获得按下的按键,和之前的两个事件一样,只有一个参数,当参数为0的时候代表鼠标左键长按,参数为1的时候代表鼠标右键长按,参数为2的时候代表鼠标中键被长按。
void Update() {
if (Input.GetMouseButton(0))
Debug.Log("Pressed left click.");
if (Input.GetMouseButton(1))
Debug.Log("Pressed right click.");
if (Input.GetMouseButton(2))
Debug.Log("Pressed middle click.");
}
public class shoot : MonoBehaviour {
public GameObject Bullet;
public float Speed = 5;
public float shootSpeed = 2; //表示每秒发射子弹的个数 俗称子弹的发射速率
public float shootTime = 0; //表示子弹的生成时间间隔 用来控制子弹的发射间隔
public float shootTimeInterval = 0;//表示子弹的间隔这个是一个固定的时间
// Use this for initialization
void Start() {
//GameObject.Instantiate(bullet, transform.position, transform.rotation);
shootTimeInterval = 1 / shootSpeed; //初始化这个时间的子弹的间隔时间
}
// Update is called once per frame
void Update()
{
shootTime += Time.deltaTime; //让子弹的时间控制器不断加等时间间隔
if (shootTime > shootTimeInterval)
{ //如果子弹发射的时间间隔超过时间控制器 那么我们就发射子弹
shootTime -= shootTimeInterval; //让子弹的时间间隔回复到初始的情况下
if (Input.GetMouseButton(0))
{
GameObject b = GameObject.Instantiate(Bullet, transform.position, transform.rotation);
Rigidbody rgb = b.GetComponent<Rigidbody>();
//rgb.velocity /*速度方向*/= transform.forward*Speed;原来代码,就算加上*Time.deltaTime,也无法跟正常射击一样,会全部卡在一个点上,收到物理的影响。
rgb.velocity /*速度方向*/= transform.TransformDirection/*变换方向*/(Vector3.forward/*指Z轴方向*/ * Speed);
}
}
}
}
场景是由物体组成的,物体是由组件组成的
Scene 场景面板
game 游戏面板
project 项目
hierarchy 属性面板
inspector
cube 正方体
sphere 圆
capsule 胶囊体
Inspector 属性面板 检视面板
Project工程面板
Hierarchy层级面板
Scene 场景面板
Game 游戏面板
创建的工程对应的文件
目录底下
ASSETS 资源(声音 图片 代码)
Library 库
projectSettings 资源设置
TEMP 临时文件夹
窗口完成开发
主要窗口是5个
窗口都在Window打开
1、SCENE 场景都在这个窗口显示
2.Game 游戏运行起来之后的窗口
3.Project 工程的资源 (比如声音图片)
4.Hierarchy当前场景下有哪些东西
5.Inspecrot 属性窗口 属性面板 检视面板
Ctrl+移动:单位1移动
视野移动:按下滑轮
视野旋转:Atl+左键;右键
1、forwar,z=1,x=0,y=0
2、velocity=方向*速度大小
C#脚本
每zhen
scyipts
rgd.velocity=trans.forwad*speed
prefabs yu she
Materials
scenes
scripts
I
Debug.log("--hello unity");
1ljmju3axWZuzupxPE4_umg
场景由物体组成
物体之间存在父子关系
游戏物体由组件组成
transform:position、旋转角度、大小
组件分为:信息组件、功能组件
设置墙体,设置子弹,设置相机位置,子弹从相机位置产生(Instantiate) (子弹和墙需有刚体和碰撞体),给子弹速度(velocity)控制相机左右及上下移动即可(transform.Translate)