Unity3D 物体移动到点击位置
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 物体移动到点击位置的更多相关文章
- unity3d 让物体移动到点击位置
using UnityEngine; using System.Collections; public class test : MonoBehaviour { //在场景中鼠标点击地面后,角色可以移 ...
- u3d 鼠标点击位置,物体移动过去。 U3d mouse clicks position, objects move past.
u3d 鼠标点击位置,物体移动过去. U3d mouse clicks position, objects move past. 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱: ...
- Unity 3D物体的点击事件响应以及NGUI坐标和世界坐标的互相转换
Unity 版本:4.5 NGUI版本:3.6.5 参考链接:http://game.ceeger.com/Script/Camera/Camera.ScreenPointToRay.html,Uni ...
- Phaser3游戏三角学应用--一只跟随屏幕点击位置游动的鱼
fish fish 资源图: fish-136x80.png undersea-bg.png 代码 var config = { type: Phaser.AUTO, parent: 'iFiero' ...
- (二)Three光线检测-实现摄像机向鼠标点击位置滑动动画
(二)Three.js光线检测 摘要:使用three.js中的光线检测 Raycaster() ,实现一下效果: 通过点击处的坐标,修改摄像机位置,实现摄像机由远及近的过渡动态效果(由远景到近景) 1 ...
- IOS ScrollView放大缩小点击位置并居中
项目中的一个优化案例,提升用户体验,对地铁线路图点击放大.缩小,并且点击位置居中: 正常ScrollView 我们点击某一点比如屏幕右侧,想要点的位置向左移动到中心位置,很简单只有算出该点位置距中心位 ...
- js 获取页面高度和宽度(兼容 ie firefox chrome),获取鼠标点击位置
<script> //得到页面高度 var yScroll = (document.documentElement.scrollHeight >document.documentEl ...
- win10 uwp 右击浮出窗在点击位置
本文主要让MenuFlyout出现在我们右击位置. 我们一般使用的MenuFlyout写在前台,写在Button里面,但是可能我们的MenuFlyout显示的位置和我们想要的不一样. 通过使用后台写S ...
- 每天一个JavaScript实例-铺货鼠标点击位置并将元素移动到该位置
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
随机推荐
- python Tkinter 全屏显示
#! /usr/bin/env python # -*- coding: utf-8 -*- import Tkinter as tk class FullScreenApp(object): def ...
- nginx的请求接收流程(一)
今年我们组计划写一本nginx模块开发以及原理解析方面的书,整本书是以open book的形式在网上会定时的更新,网址为http://tengine.taobao.org/book/index.htm ...
- C#正则表达式匹配任意字符
原文:C#正则表达式匹配任意字符 不得不说正则很强大,尤其在字符串搜索上 匹配任意字符,包括汉字,换行符: [\s\S]*. 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 在WPF中自定义你的绘制(四)
原文:在WPF中自定义你的绘制(四) 在WPF中自定义你的绘制(四) ...
- haproxy path_end不能忽略
C:\>ping www.zjtest7.com 正在 Ping www.zjtest7.com [192.168.32.82] 具有 32 字节的数据: 来自 192.168.32.82 的回 ...
- codevs1039 数的划分
题目描述 Description 将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序). 例如:n=7,k=3,下面三种划分方案被认为是相同的. 1 1 5 1 5 1 5 1 1 ...
- cf466C Number of Ways
C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- NODE.JS安装配置
- 杭电oj 1328
Tips:本题中没有任何难度,直接按普通逻辑进行计算即可. #include<stdio.h> #include<string.h> ]={'A','B','C','D','E ...
- zlog
zlog源码包下载地址https://github.com/HardySimpson/zlog zlog使用手册http://blog.csdn.net/yangzhenzhen/article/de ...