using UnityEngine;
using System.Collections;

public class MoveToClick : MonoBehaviour
{
public GameObject play;
public Vector3 temPos;
public bool isMoving;
public Quaternion rotation;
// Use this for initialization
void Start()
{
play = GameObject.Find("Hero");
print(play);
}

// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit, 100))
{
print(hit.collider.name);
if (hit.collider.gameObject.name == "Plane")
{
Debug.Log(hit.point);

temPos = new Vector3(hit.point.x, play.transform.position.y, hit.point.z);

if (Vector3.Distance(play.transform.position, temPos) > 0.1)
{
isMoving = true;
}
}
}
}

if (Vector3.Distance(play.transform.position, temPos) <= 0.1)
{
isMoving = false;
}

if (isMoving)
{
turn(temPos);
this.GetComponent<CharacterController>().SimpleMove((temPos - play.transform.position).normalized * 5f);
}

}

void turn(Vector3 look)
{
rotation = Quaternion.LookRotation(temPos - play.transform.position, Vector3.up);
play.transform.rotation = Quaternion.Slerp(play.transform.rotation, rotation, Time.deltaTime * 6.0f);
}
}

Unity3D 物体移动到点击位置的更多相关文章

  1. unity3d 让物体移动到点击位置

    using UnityEngine; using System.Collections; public class test : MonoBehaviour { //在场景中鼠标点击地面后,角色可以移 ...

  2. u3d 鼠标点击位置,物体移动过去。 U3d mouse clicks position, objects move past.

    u3d 鼠标点击位置,物体移动过去. U3d mouse clicks position, objects move past. 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱: ...

  3. Unity 3D物体的点击事件响应以及NGUI坐标和世界坐标的互相转换

    Unity 版本:4.5 NGUI版本:3.6.5 参考链接:http://game.ceeger.com/Script/Camera/Camera.ScreenPointToRay.html,Uni ...

  4. Phaser3游戏三角学应用--一只跟随屏幕点击位置游动的鱼

    fish fish 资源图: fish-136x80.png undersea-bg.png 代码 var config = { type: Phaser.AUTO, parent: 'iFiero' ...

  5. (二)Three光线检测-实现摄像机向鼠标点击位置滑动动画

    (二)Three.js光线检测 摘要:使用three.js中的光线检测 Raycaster() ,实现一下效果: 通过点击处的坐标,修改摄像机位置,实现摄像机由远及近的过渡动态效果(由远景到近景) 1 ...

  6. IOS ScrollView放大缩小点击位置并居中

    项目中的一个优化案例,提升用户体验,对地铁线路图点击放大.缩小,并且点击位置居中: 正常ScrollView 我们点击某一点比如屏幕右侧,想要点的位置向左移动到中心位置,很简单只有算出该点位置距中心位 ...

  7. js 获取页面高度和宽度(兼容 ie firefox chrome),获取鼠标点击位置

    <script> //得到页面高度 var yScroll = (document.documentElement.scrollHeight >document.documentEl ...

  8. win10 uwp 右击浮出窗在点击位置

    本文主要让MenuFlyout出现在我们右击位置. 我们一般使用的MenuFlyout写在前台,写在Button里面,但是可能我们的MenuFlyout显示的位置和我们想要的不一样. 通过使用后台写S ...

  9. 每天一个JavaScript实例-铺货鼠标点击位置并将元素移动到该位置

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. deepin添加新的打开方式软件

    在/usr/share/applications文件夹中,你可以先打开一个其他的软件比如geany,然后根据geany的配置配置你所需要的新软件

  2. Nginx 配置指令的执行顺序(八)

    前面我们详细讨论了 rewrite.access 和 content 这三个最为常见的 Nginx 请求处理阶段,在此过程中,也顺便介绍了运行在这三个阶段的众多 Nginx 模块及其配置指令.同时可以 ...

  3. CSS 总结

    CSS 积累总结 1. ::Selection 选择器 使被选中的文本成为灰色: ::selection { color:#CCC; background:red; --- 选中背景颜色变成红色 } ...

  4. python学习day4

    目录 一.迭代器 二.yield生成器 三.装饰器 四.递归 五.基础算法 迭代器 #1.在不使用for循环的情况下 li = [11 ,22, 33, 44] #count = len(li) #s ...

  5. AprioriTID algorithm

    What is AprioriTID? AprioriTID is an algorithm for discovering frequent itemsets (groups of items ap ...

  6. 从setTimeout到浏览器线程机制

    看高性能javascipt 这本书时,看到这么一句话: Putting scripts at the top of the page in this way typically leads to a ...

  7. GO的GDB调试

    GoLang语言,学了很久,一直觉得它单步调试有较多问题,最近才知道自已对它了解得太少了.原来GO语言对GDB的版本是至少为gdb7以上,才能比较好的打印任意变量,如果低于这个版本,则才会出一些问题. ...

  8. SQL Server中各个系统表的作用

    sysaltfiles            主数据库               保存数据库的文件 syscharsets         主数据库               字符集与排序顺序 s ...

  9. HTML5 Audio时代的MIDI音乐文件播放

    大家都知道,HTML5 Audio标签能够支持wav, webm, mp3, ogg, acc等格式,但是有个很重要的音乐文件格式midi(扩展名mid)却在各大浏览器中都没有内置的支持,因为mid文 ...

  10. JqueryeasyUI选项卡选择判定更改内部Iframe地址

    1.tabs的常用操作 //1.判断tab是否存在. var currtab = $('#tabs').tabs('getSelected'); //2.判断点击的tab是否是当前选中的tab. va ...