【Unity学习】
文章目录
- c#脚本基本语句
- 一、游戏启动的基本语句
- 二、游戏运行的基本语句
- 1.游戏运行的基本语句
- 2.对物体的相关操作
- 三、Unity组件的一些方法
- 1.Rigidbody组件
- 2.Collider组件
- 3.Audio Source组件
- 四、Unity脚本常用的方法实现
- 1.角色移动和视角移动
c#脚本基本语句
一、游戏启动的基本语句
publicGameObjectgo;publicGameObject[]gos;//Start()方法在游戏启动时执行一次。voidStart(){//设置游戏帧率Application.targetFrameRate=120;//一般设置为60/120//鼠标箭头锁定Cursor.lockState=CursorLockMode.Locked;//获取物体currentGameObject=this.gameObject;//获取脚本当前物体go=GameObject.Find("名称");//根据名称获取物体go=GameObject.FindWithTag("标签");//根据标签获取物体gos=GameObject.FindGameObjectsWithTag("标签");//根据标签获取物体数组//创建游戏物体//四元数,Unity定义旋转要用四元数,Euler方法能将Vector3变量转换为四元数quaternionq=Quaternion.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");//返回值只有-1,0或1Input.GetAxis("Vertical");//垂直轴,按下W/S或上下方向键,返回值为-1到0到1平滑过度Input.GetAxisRaw("Vertical");//返回值只有-1,0或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(){//创建物体GameObjectgo=Instantiate(goPrefab,transform.position,transform.rotation);//物体信息更改go.name="name1";go.transform.position=newVector3(10,10,10);go.transform.eulerAngles=newVector3(0,45,0);go.transform.localScale=newVector3(2,2,2);//物体移动(第二个参数选择坐标系),两种皆可go.transform.position+=positionTarget;go.transform.Translate(positionTarget*Time.deltaTime,Space.Self);//物体旋转(第二个参数选择坐标系),两种皆可go.transform.eulerAngles+=rotationTarget;go.transform.Rotate(rotationTarget*Time.deltaTime,Space.Self);//获取组件go.GetComponent<组件名称>();//物理模拟rb.AddForce(newVector3(100,0,0)*Time.deltaTime);//设置刚体推力rb.linearVelocity=newVector3(0,0,0);//设置刚体速度,可以实现带碰撞的移动rb.angularVelocity=newVector3(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.resource=bgm;//切换音频ad.Play();//播放音频四、Unity脚本常用的方法实现
1.角色移动和视角移动
键盘操控移动,鼠标控制视角的实现
publicVector3positionTarget;publicVector3rotationTarget;publicfloatspeedMove=3f;publicfloatspeedRotate=200f;//角色移动publicvoidmove(){positionTarget.x=Input.GetAxis("Horizontal");positionTarget.z=Input.GetAxis("Vertical");this.transform.Translate(positionTarget*speedMove*Time.deltaTime);rotationTarget.y=Input.GetAxis("Mouse X");this.transform.Rotate(rotationTarget*speedRotate*Time.deltaTime);}摄像机射线碰撞检测,鼠标点击操控移动的实现
publicNavMeshAgentagent;//角色移动publicvoidmove(){if(Input.GetMouseButtonDown(1))//检测鼠标右键输入{Rayray=Camera.main.ScreenPointToRay(Input.mousePosition);//从主摄像机向鼠标点击位置发出射线if(Physics.Raycast(ray,outRaycastHithit))//射线检测{if(hit.collider.CompareTag("标签"))//通过标签检测是否点击到目标地板{agent.SetDestination(hit.point);//设置目标点}}}}