【Unity学习】

【Unity学习】
文章目录c#脚本基本语句一、游戏启动的基本语句二、游戏运行的基本语句1.游戏运行的基本语句2.对物体的相关操作三、Unity组件的一些方法1.Rigidbody组件2.Collider组件3.Audio Source组件四、Unity脚本常用的方法实现1.角色移动和视角移动c#脚本基本语句一、游戏启动的基本语句publicGameObjectgo;publicGameObject[]gos;//Start()方法在游戏启动时执行一次。voidStart(){//设置游戏帧率Application.targetFrameRate120;//一般设置为60/120//鼠标箭头锁定Cursor.lockStateCursorLockMode.Locked;//获取物体currentGameObjectthis.gameObject;//获取脚本当前物体goGameObject.Find(名称);//根据名称获取物体goGameObject.FindWithTag(标签);//根据标签获取物体gosGameObject.FindGameObjectsWithTag(标签);//根据标签获取物体数组//创建游戏物体//四元数Unity定义旋转要用四元数,Euler方法能将Vector3变量转换为四元数quaternionqQuaternion.Euler(0,0,0);//实例一个物体定义位置和旋转再加一个参数可以定义父物体//这里的go一般放预制体Instantiate(go,newVector3(0,0,0),q);}二、游戏运行的基本语句1.游戏运行的基本语句voidUpdate(){//输出打印Debug.Log(hello world!);Debug.Log(当前物体名称:go.name);Debug.Log(当前物体位置:go.transform.position);Debug.Log(当前物体旋转:go.transform.rotation.eulerAngles);Debug.Log(当前物体大小:go.transform.localScale);//检测输入Input.GetKeyDown(KeyCode.A)//按键按下Input.GetKeyUp(KeyCode.A)//按键抬起Input.GetKey(KeyCode.A)//按住不松Input.GetAxis(Horizontal);//水平轴,按下A/D或左右方向键,返回值为-1到0到1的平滑过度Input.GetAxisRaw(Horizontal);//返回值只有-10或1Input.GetAxis(Vertical);//垂直轴,按下W/S或上下方向键,返回值为-1到0到1平滑过度Input.GetAxisRaw(Vertical);//返回值只有-10或1Input.GetKeyDown(KeyCode.Mouse0);//按下鼠标左键,两种皆可Input.GetMouseButtonDown(0);Input.GetKeyDown(KeyCode.Mouse1);//按下鼠标右键,两种皆可Input.GetMouseButtonDown(1);Input.GetAxis(Mouse ScrollWheel);//鼠标滚轮输入Input.GetAxis(Mouse X);//鼠标横向移动Input.GetAxis(Mouse Y);//鼠标竖向移动Debug.Log(鼠标位置为:Input.mousePosition);//鼠标位置信息}2.对物体的相关操作publicTransformtransform;publicGameObjectgoPrefab;publicVector3positionTarget;publicVector3rotationTarget;publicRigidbodyrb;publicvoidobject_transform(){//创建物体GameObjectgoInstantiate(goPrefab,transform.position,transform.rotation);//物体信息更改go.namename1;go.transform.positionnewVector3(10,10,10);go.transform.eulerAnglesnewVector3(0,45,0);go.transform.localScalenewVector3(2,2,2);//物体移动(第二个参数选择坐标系),两种皆可go.transform.positionpositionTarget;go.transform.Translate(positionTarget*Time.deltaTime,Space.Self);//物体旋转(第二个参数选择坐标系),两种皆可go.transform.eulerAnglesrotationTarget;go.transform.Rotate(rotationTarget*Time.deltaTime,Space.Self);//获取组件go.GetComponent组件名称();//物理模拟rb.AddForce(newVector3(100,0,0)*Time.deltaTime);//设置刚体推力rb.linearVelocitynewVector3(0,0,0);//设置刚体速度,可以实现带碰撞的移动rb.angularVelocitynewVector3(0,0,0);//设置刚体角速度,可以实现旋转//物体销毁Destroy(go);}三、Unity组件的一些方法1.Rigidbody组件刚体组件模拟物理效果//刚体碰撞privatevoidOnCollisionEnter(Collisioncollision){//Debug.Log(collision.gameObject.name);}//刚体分离privatevoidOnCollisionExit(Collisioncollision){//Debug.Log(collision.gameObject.name);}//刚体持续接触privatevoidOnCollisionStay(Collisioncollision){//Debug.Log(collision.gameObject.name);}2.Collider组件//触发器触发privatevoidOnTriggerEnter(Colliderother){//Debug.Log(other.gameObject.name);}//触发器分离privatevoidOnTriggerExit(Colliderother){//Debug.Log(other.gameObject.name);}//触发器持续触发privatevoidOnTriggerStay(Colliderother){//Debug.Log(other.gameObject.name);}3.Audio Source组件音频播放组件//存放组件publicAudioSourcead;//存放音频publicAudioClipaclip;publicAudioClipbgm;//播放音频,参数可设置音量ad.PlayOneShot(aclip,0.5f);ad.resourcebgm;//切换音频ad.Play();//播放音频四、Unity脚本常用的方法实现1.角色移动和视角移动键盘操控移动鼠标控制视角的实现publicVector3positionTarget;publicVector3rotationTarget;publicfloatspeedMove3f;publicfloatspeedRotate200f;//角色移动publicvoidmove(){positionTarget.xInput.GetAxis(Horizontal);positionTarget.zInput.GetAxis(Vertical);this.transform.Translate(positionTarget*speedMove*Time.deltaTime);rotationTarget.yInput.GetAxis(Mouse X);this.transform.Rotate(rotationTarget*speedRotate*Time.deltaTime);}摄像机射线碰撞检测鼠标点击操控移动的实现publicNavMeshAgentagent;//角色移动publicvoidmove(){if(Input.GetMouseButtonDown(1))//检测鼠标右键输入{RayrayCamera.main.ScreenPointToRay(Input.mousePosition);//从主摄像机向鼠标点击位置发出射线if(Physics.Raycast(ray,outRaycastHithit))//射线检测{if(hit.collider.CompareTag(标签))//通过标签检测是否点击到目标地板{agent.SetDestination(hit.point);//设置目标点}}}}