transform.position = Vector3(0,5,4); renderer.material.color = Color.blue; light.intensity = 8;
5) GetComponent()命令:引用一个组件
实例代码例如以下所看到的:
#pragma strict var thelight:GameObject; var thetxt:GameObject; var theCube1:GameObject; function Start () { thelight = GameObject.Find("Spotlight"); thetxt = GameObject.FindWithTag("txt"); theCube1 = GameObject.Find("Cube1"); } function Update () { if(Input.GetKey(KeyCode.L)){ thelight.light.intensity += 0.01; thetxt.GetComponent(GUIText).text = "当前亮度:"+thelight.light.intensity; } if(Input.GetKey(KeyCode.K)){ thelight.light.intensity -= 0.01; thetxt.GetComponent(GUIText).text = "当前亮度:"+thelight.light.intensity; } if(Input.GetKey(KeyCode.S)){ theCube1.SendMessage("OnMouseDown"); //调用theCube1全部脚本中的OnMouseDown函数 } }
第一人称控制器脚本代码例如以下所看到的:
#pragma strict var speed:float=6.0; var jumpspeed:float=8.0; var gravity:float=20.0; private var movedirection:Vector3=Vector3.zero; private var grounded:boolean=false; function Start () { } function FixedUpdate () { if(grounded){ movedirection=Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")); //Transforms direction from local space to world space. movedirection=transform.TransformDirection(movedirection); movedirection *= speed; if(Input.GetButton("Jump")){ movedirection.y = jumpspeed; } } movedirection.y -= gravity*Time.deltaTime; //Move command var controller:CharacterController = GetComponent(CharacterController); var flags = controller.Move(movedirection*Time.deltaTime); //CollisionFlags.CollidedBelow 底部发生了碰撞 //CollisionFlags.CollidedSides 四周发生了碰撞 //CollisionFlags.CollidedAbove 顶端发生了碰撞 grounded = (flags & CollisionFlags.CollidedBelow)!=0; } //强制Unity为本脚本所依附的物体添加�角色控制器组件 @script RequireComponent(CharacterController)
在3DMax(.fbx文件)把显示单位和系统单位设置为cm,再导入到Unity3D(单位为m)中时,则大小同样。
玩家撞到门时,门才干开。
其相关代码例如以下所看到的,须要对模型的动画进行切割,下面脚本绑在玩家身上:
#pragma strict private var doorisopen:boolean = false; private var doortimer:float = 0.0; private var currentdoor:GameObject; var door_open_time:float = 5.0; var door_open_sound:AudioClip; var door_shut_sound:AudioClip; function Update(){ if(doorisopen){ doortimer += Time.deltaTime; if(doortimer > door_open_time) { doortimer = 0.0; door(false,door_shut_sound,"doorshut",currentdoor); } } } //检測玩家是否与门相撞 function OnControllerColliderHit(hitt:ControllerColliderHit){ //hitt为与玩家相撞的碰撞体 print("test1"); if(hitt.gameObject.tag == "playerDoor" && doorisopen == false){ print("test2"); door(true,door_open_sound,"dooropen",hitt.gameObject); currentdoor = hitt.gameObject; } } function door(doorcheck:boolean,a_clip:AudioClip,ani_name:String,thisdoor:GameObject){ doorisopen=doorcheck; thisdoor.audio.PlayOneShot(a_clip); //声音取消3D效果,否则无法播放 thisdoor.transform.parent.animation.Play(ani_name); }
玩家必须面对门时,光线投射到门时,门才干开。
其检測碰撞代码例如以下所看到的,下面脚本绑在玩家身上:
function Update(){ var hit:RaycastHit; //hit为与投射光线相遇的碰撞体 if(Physics.Raycast(transform.position,transform.forward,hit,3)){ print("collider happen" + hit.collider.gameObject.tag); if(hit.collider.gameObject.tag == "playerDoor"){ var currentdoor:GameObject = hit.collider.gameObject; currentdoor.SendMessage("doorcheck"); print("call doorcheck"); } } }
门身上的脚本例如以下所看到的:
#pragma strict private var doorisopen:boolean = false; private var doortimer:float = 0.0; //private var currentdoor:GameObject; var door_open_time:float = 5.0; var door_open_sound:AudioClip; var door_shut_sound:AudioClip; function Update(){ if(doorisopen){ doortimer += Time.deltaTime; if(doortimer > door_open_time) { doortimer = 0.0; door(false,door_shut_sound,"doorshut"); } } } function doorcheck(){ if(!doorisopen){ door(true,door_open_sound,"dooropen"); } } function door(doorcheck:boolean,a_clip:AudioClip,ani_name:String){ doorisopen=doorcheck; audio.PlayOneShot(a_clip); //声音取消3D效果,否则无法播放 transform.parent.animation.Play(ani_name); }
仅仅要一进入触发区域,门就能够打开。须要特别注意调整小房子的Box Collider的大小和位置。
下面脚本绑在门所在的小房子上:
#pragma strict function OnTriggerEnter (col:Collider) { //col为闯入此区域的碰撞体 if(col.gameObject.tag == "Player"){ transform.FindChild("door").SendMessage("doorcheck"); } }
检測两个普通碰撞体之间的碰撞,须要发力,才干发生碰撞。不能与第一人称控制器发生碰撞。
1) OnControllerColliderHit
专门提供给角色控制器使用,以供角色控制器检測与其他物体发生了碰撞。
它与触发模式的碰撞体不能发生碰撞,并且还会穿过其物体。
下面脚本绑定在玩家(第一人称控制器)身上。
function OnControllerColliderHit (hit:ControllerColliderHit) { if(hit.gameObject.name != "Plane") gameObject.Find("wenzi").guiText.text = hit.gameObject.name; }
它不是一个事件,而是一个命令,能够须要的地方调用它来发射一条光线来进行检測。
能够与触发模式的碰撞体发生碰撞,但也会穿过其物体。
下面脚本绑定在玩家(第一人称控制器)身上。
#pragma strict var hit:RaycastHit; function Update () { if(Physics.Raycast(transform.position,transform.forward,hit,2)){ if(hit.collider.gameObject.name != "Plane"){ gameObject.Find("wenzi").guiText.text = hit.collider.gameObject.name; } } }
下面脚本绑定在与第一人称控制器相撞的物体上。
#pragma strict function OnTriggerEnter (col:Collider) { gameObject.Find("wenzi").guiText.text = col.gameObject.name; }
4) OnCollisionEnter
检測两个普通碰撞体之间的碰撞,须要发力,才干发生碰撞。不能与第一人称控制器发生碰撞。
GameObject有Audio Source组件,且在Audio Source组件中设置了Audio Clip。
var door_open_sound:AudioClip; //在检測面板中设置audio clip;当然必须有Audio Source组件,但不须要设置Audio Clip function myAudioPlay(){ audio.PlayOneShot(door_open_sound); //声音取消3D效果,否则无法播放 }
此种播放方式最佳,节约资源。
var collectSound:AudioClip; //在检測面板中设置其值 function pickupCell(){ //暂时申请一个AudioSource,播放完了即时销毁 AudioSource.PlayClipAtPoint(collectSound,transform.position); }
Destroy(gameObject); //即时销毁物体
Destroy(gameObject,3.0); // 3秒之后销毁物体
方法一:
#pragma strict var throwsound:AudioClip; var coconut_bl:Rigidbody; //为预制物体 function Update () { if(Input.GetButtonDown("Fire1")){ audio.PlayOneShot(throwsound); var newcoconut:Rigidbody = Instantiate(coconut_bl,transform.position, transform.rotation); newcoconut.velocity=transform.forward*30.0; //设置刚体速度 } }
使用这两个碰撞器碰撞时不发生效果
Physics.IgnoreCollision(transform.root.collider,newcoconut.collider,true);
协同程序:类似子线程,在执行本代码的同一时候,运动脚本中的其他代码。
#pragma strict // this script will be attached to target object private var beenhit:boolean = false; private var targetroot:Animation; var hitsound:AudioClip; var resetsound:AudioClip; var resettime:float=3.0; function Start () { targetroot = transform.parent.transform.parent.animation; } function OnCollisionEnter (theobject:Collision) { if(beenhit == false && theobject.gameObject.name == "coconut"){ StartCoroutine("targethit"); //协同程序:相似子线程,在执行本代码的同一时候,运动脚本中的其他代码 } } function targethit(){ audio.PlayOneShot(hitsound); // 播放声音 targetroot.Play("down"); // 播放动画 beenhit = true; yield new WaitForSeconds(resettime); // wait for 3s audio.PlayOneShot(resetsound); targetroot.Play("up"); beenhit=false; }
#pragma strict var obj:GameObject; var myskin:GUISkin; function Start () { } function Update () { } function OnGUI(){ //GUI.skin = myskin; if(GUILayout.Button("Add_Component",GUILayout.Height(40),GUILayout.Width(110))){ obj.AddComponent("xuanzhuan"); // xuanzhuan为一个脚本xuanzhuan。js } if(GUILayout.Button("Del_Component",GUILayout.Height(40),GUILayout.Width(110))){ var script:Object = obj.GetComponent("xuanzhuan"); Destroy(script); } }
粒子系统是在三维空间中渲染出来的二维图像,主要用于表现烟、火、水滴和落叶等效果。
粒子系统由:粒子发射器、粒子动画器和粒子渲染器组成。