- <pre name="code" class="javascript"></pre><pre name="code" class="javascript"></pre><pre name="code" class="javascript">var moveSpeed:int=5;//player移动速度
- var player:Transform;//定义一个人物的Transform
- private var endposition : Vector3;
- function Start()
- {
- endposition = player.transform.position;
- }
- function Update ()
- {
- if(Input.GetButtonUp("LeftMouse")){ //LeftMouse是在inputManager中设置的,左键值为mouse 0
- PlayerMove();
- }
- var targetposition=player.TransformPoint(Vector3(0,48.8,-30));
- transform.position=targetposition;//相机的目标位置,这两句代码的作用是让人物一直处于相机的视野下
- if(endposition != player.transform.position){
- player.position=Vector3.MoveTowards(player.position,endposition,Time.deltaTime*moveSpeed);
- }
- }
- function PlayerMove()
- {
- var cursorScreenPosition:Vector3=Input.mousePosition;//鼠标在屏幕上的位置
- var ray:Ray=Camera.main.ScreenPointToRay(cursorScreenPosition);//在鼠标所在的屏幕位置发出一条射线(暂名该射线为x射线)
- var hit:RaycastHit;
- if(Physics.Raycast(ray,hit)){
- if(hit.collider.gameObject.tag=="Terrain"){//设置地形Tag为Terrain
- endposition = hit.point;
- }
- }
- }
- </pre><br>
- <br>
本文来自:http://blog.csdn.net/we000636/article/details/7476690
- <pre></pre>
- <pre></pre>