unity3d 让物体移动到点击位置
using UnityEngine;
using System.Collections; public class test : MonoBehaviour {
//在场景中鼠标点击地面后,角色可以移动到目标位置 private Vector3 target;
private bool isOver = true;
public float speed;
void Start () { } void Update () {
if(Input.GetMouseButtonDown())
{
print("MouseDown");
//1. 获取鼠标点击位置
//创建射线;从摄像机发射一条经过鼠标当前位置的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//发射射线
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo))
{
//获取碰撞点的位置
if (hitInfo.collider.name == "Plane")
{
target = hitInfo.point;
target.y = 0.5f;
isOver = false;
}
}
//RaycastHit[] hitAll = Physics.RaycastAll(ray, 1000);
//foreach(RaycastHit hitInfo in hitAll)
//{
// print(hitInfo.collider.name);
// if (hitInfo.collider.name == "Plane")
// {
// target = hitInfo.point;
// target.y = 0.5f;
// isOver = false;
// }
//}
} //2. 让角色移动到目标位置
MoveTo(target);
} //让角色移动到目标位置
private void MoveTo(Vector3 tar)
{
if(!isOver)
{
Vector3 offSet = tar - transform.position;
transform.position += offSet.normalized * speed * Time.deltaTime;
if(Vector3.Distance(tar, transform.position)<0.5f)
{
isOver = true;
transform.position = tar;
}
} }
}
unity3d 让物体移动到点击位置的更多相关文章
- Unity3D 物体移动到点击位置
using UnityEngine;using System.Collections; public class MoveToClick : MonoBehaviour{ public GameObj ...
- 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' ...
- unity3D 游戏物体同时绑定单击、双击事件
前言 在unity中我们常用的获取鼠标点击的方法有 在3D场景中,一般用在Update方法中,每一帧调用 void Update(){ )){ Debug.log("鼠标左键点击" ...
- (二)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 ...
随机推荐
- printf 打印较长字符
- DD打卡
一.安装逍遥安卓模拟器 二.安装钉钉 三.设置当前GPS座标 位置模拟器: 链接: https://pan.baidu.com/s/1TC5QkrGAgHOJWtzJnX6vhA 提取码: bpu8 ...
- 【转载】java 监听文件或者文件夹变化的几种方式
1.log4j的实现的文件内容变化监听 package com.jp.filemonitor; import org.apache.log4j.helpers.FileWatchdog; public ...
- iterm2 快捷键设置
单词跳转 设置option+ 左右键
- css 陌生属性
记录一些我之前没见过属性 1.width:100vh 100vh 2.min-height:calc(100vh + 51px);cale 3.:nth-child nth-child 和 :n ...
- JavaScript获取日期方法
var time = new Date(); //当前时间 var year = time.getFullYear();//当前年份 var month = time.getMonth()+1; // ...
- 关于虚拟机中克隆的linux为什么不能开启网络服务
将centos克隆了一份,启动后并配置好文件,发现网络服务中只有lo(loopback),而网卡(eth0)没有启动,一开始以为是通信模式(bridged,NAT,host-only)的选择问题,最后 ...
- 重命名文件及html
import os import nltk from bs4 import BeautifulSoup as bs def get_txt_name_from_bak_name(bak_name): ...
- hdu 1384 查分约束
#include<stdio.h> /* 要善于挖掘隐含条件 dis[v]-dis[u]>=bian[i].w;一个条件(u,v,bian[i].w); dis[i+1]>=d ...
- Maven使用package打包Spring Boot时出现:Unable to find a single main class from the following candidates的问题解决
问题如下: [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE ...