纳金网

标题: 人物旋转至鼠标点点击点的代码 [打印本页]

作者: 烟雨    时间: 2015-9-27 01:30
标题: 人物旋转至鼠标点点击点的代码
人物旋转至鼠标点点击点的代码:
  1. using UnityEngine;
  2. using System.Collections;

  3. public class PlayerRotation : MonoBehaviour {

  4.         Quaternion targetRotation;
  5.         public float RotationSpeed = 25f;//旋转速度.
  6.         bool isRotation = false;//开始旋转.
  7.         void Update ()
  8.         {
  9.                 if (Input.GetMouseButtonDown (0)) {
  10.                         //从摄像机的原点向鼠标点击的对象身上设法一条射线.
  11.                         Ray ray = YHY.Instance.camera3d.ScreenPointToRay (Input.mousePosition);
  12.                         RaycastHit hit;
  13.                         //当射线彭转到对象时.
  14.                         if (Physics.Raycast (ray, out hit)) {
  15.                                 Debug.Log ("发出射线");
  16.                                 //  目前场景中只有地形.
  17.                                 //判断一下当前射线碰撞到的对象是否为地形。
  18.                                 if (hit.collider.gameObject.tag == "Terrain") {//设置地形Tag为Terrain.
  19.                                         //Debug.Log("射线碰撞到地形");
  20.                                        
  21.                                         Vector3 targetPoint = Vector3.zero;//hitdist.point;
  22.                                         targetPoint.x = hit.point.x;
  23.                                         targetPoint.y = transform.position.y;
  24.                                         targetPoint.z = hit.point.z;
  25.                                         targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
  26.                                         isRotation =true;
  27.                                 }}}
  28.                 //实际旋转.
  29.                 if(isRotation)
  30.                 this.transform.rotation = Quaternion.Lerp (this.transform.rotation, targetRotation, Time.deltaTime * RotationSpeed);
  31.         }
  32. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5