查看: 471|回复: 0
打印 上一主题 下一主题

[其他] 人物旋转至鼠标点点击点

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53456
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-8-28 01:14:57 |只看该作者 |倒序浏览
人物旋转至鼠标点点击点
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-29 15:01 , Processed in 0.079451 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部