unity3d射线控制移动
看看效果图

代码:
using UnityEngine;
using System.Collections; public class T2 : MonoBehaviour
{ // Use this for initialization
void Start()
{ } //定义射线
Ray ray;
RaycastHit hit;
//是否移动
bool isMove;
//移动方向
Vector3 dir;
Vector3 pos; // Update is called once per frame
void Update()
{
//float v = Input.GetAxis("Vertical");
//float h = Input.GetAxis("Horizontal");
//transform.Translate(transform.forward * v * Time.deltaTime); //transform.Rotate(Vector3.up * h); //鼠标左键按下的时候
if (Input.GetMouseButtonDown())
{
//从摄像机发一条射线
ray = Camera.main.ScreenPointToRay(Input.mousePosition); ;
//如果检测到物体(射线用来检测)
if (Physics.Raycast(ray, out hit))
{
//将碰撞点的Y值变为transform的Y值
pos = new Vector3(hit.point.x, transform.position.y, hit.point.z); //hit.point是物体的位置
transform.LookAt(hit.point);
//开始移动
isMove = true;
//求移动方向
//dir = (hit.point - transform.position).normalized; dir = (pos - transform.position).normalized; }
}
//如果开始移动
if (isMove)
{
//沿着世界坐标的某一个位置移动
transform.Translate(dir * Time.deltaTime * , Space.World);
//如果距离小于0.5m的时候,停止移动
if (Vector3.Distance(transform.position, hit.point) < 0.5f)
{
isMove = false;
}
} }
}
当然。出来用LookAt还可以用LookRotation
using UnityEngine;
using System.Collections; //加刚体
public class movee : MonoBehaviour
{ // Use this for initialization
void Start()
{ } bool isMove;
Vector3 dir; RaycastHit hit; Vector3 hitPoint; Quaternion rotation; Quaternion originRatation;
float timer; // Update is called once per frame
void FixedUpdate()
{
if (Input.GetMouseButtonDown())
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit))
{ hitPoint = new Vector3(hit.point.x, transform.position.y, hit.point.z); //transform.LookAt(hit.point); //dir = (hit.point - transform.position).normalized; dir = hitPoint - transform.position; rotation = Quaternion.LookRotation(dir); originRatation = transform.rotation; isMove = true; timer = ; }
}
if (isMove)
{
//transform.Translate(dir * Time.deltaTime * 5, Space.World); timer += Time.fixedDeltaTime; //if (Vector3.Distance(transform.position, hit.point) < 0.5) isMove = false; //慢慢旋转过去,这不是匀速的。
//transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.fixedDeltaTime*5); //既然想匀速的话,那么起点和终点不变。时间变化
transform.rotation = Quaternion.Lerp(originRatation, rotation, timer); //transform.rotation = rotation; //角度直接等于这个 if (Quaternion.Angle(transform.rotation, rotation) < )
{
transform.rotation = rotation;
isMove = false;
} }
} public void Update()
{ }
}
unity3d射线控制移动的更多相关文章
- 【转】Unity3D 射线Ray实现点击拾取
游戏中经常会有鼠标移动到某个对象上来拾取它的功能,我们可以用Unity3D中的射线Ray实现这一效果.原理是在我们鼠标的位置,从屏幕射出一条射向世界空间的射线,当这条射线碰撞到我们需要拾取的对象时,我 ...
- (转)在Unity3D中控制动画播放
用Unity3D也算是好久了,但是每次做项目总还是能学到新的东西.这次做一个TPS的项目就遇到了这样一个问题,如何同时在上下半身播放不同的动画?解决方法其实是很简单,但由于对于动画资源的了解不足导致问 ...
- Unity3D 射线指定层获取GameObject 注意 LayerMask
这一篇是纯技术讨论,看过我前面文章的童鞋应该清楚,我的奔跑是靠鼠标响应的,鼠标点到哪就跑到哪,后来又有了界面,麻烦就来了,我的界面居然能点 穿.我不希望点界面的时候还能点到界面后面的地面上,角色傻不拉 ...
- 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
原地址:http://www.cnblogs.com/xuling/archive/2013/03/04/2943154.html 最近开始研究U3D,它的强大就不多说了, 今天研究了研究射线相关东西 ...
- unity3d 射线扫描 忽略图层
原地址:http://blog.csdn.net/w88193363/article/details/38331205 函数说明 static RaycastHit2D[] RaycastAll(Ve ...
- unity3D射线检测敌人是否在前方
//发射一条射线,去检测玩家是否在自己面前,用来随后的发射炮弹 bool isForward = false; var TranformShootPoint = ai.WorkingMemory.Ge ...
- unity3d 射线的原理,基础用法
射线:射线是3D世界中一个点向一个方向发射的一条无终点的线,在发射轨迹中与其他物体发生碰撞时,它将停止发射 . Ray射线类和RaycastHit射线投射信息类是射线中常用的两个工具类. 用途:射线多 ...
- Unity3D Animator控制参数和添加事件
Animator控制参数和添加事件 using UnityEngine; using System.Collections; public class AniControl : MonoBehavio ...
- unity3d之控制人物转向移动并播放动画
tip:transition 勾选Has Exit Time B动画播放完毕后就可以自己返回A不用代码控制.因为想做一个小人静止时 隔两秒会摆动小手的特效. 附上代码参考: using UnityEn ...
随机推荐
- css学习_css书写规范
css书写规 1.空格规范: div { color: red; } 2.选择器规范 3.属性 属性定义要另起一行 属性定义后必须以分号结尾 div { color: red; font-size: ...
- [No0000F2]ip安全监视器
IPSec快速式策略 @echo off :again set num= set fastpolicyname= set issoft= set livetime= set fps= setlocal ...
- iOS RSA非对称加密测试流程
非对称加密需要两把钥匙:公钥和秘钥. 单向加密:一般情况下服务器会持有秘钥和公钥,那该怎么使用呢?以注册场景为例,最初服务器持有公钥和密钥. 用户注册时不是直接发送用户名,密码,验证码等明文信息给服务 ...
- [security] security engine things
1. luarock luarock 之于 lua,就好比 pip 之于 python https://luarocks.org/ 2. lua的库 [root@base package]# ls ...
- 《linux 用户管理》- useradd/userdel/usermod/groupadd/who/w
一:概念 在 Linux 中,使用一个 32位整数 来记录每一个用户(USER ID 简单 UID),这意味着在 Linux 中,可以有 40亿 个不同的用户. 系统. 在 /etc/passwd ...
- Android SDK下载和更新失败的如何解决
一.scenario: 在部署Appium环境搭建时,安装SDK Manager.exe,遇到Android SDK下载更新失败. 报错信息如下图: Failed to fetch URL https ...
- mysql之show engine innodb status解读(转)
add by zhj: 我第一次知道这个命令是线上服务出了问题,然后同事用这个命令去查看死锁.但用这个命令看死锁有一定的局限性,它只能看到最后一次死锁, 而且只能看到死锁环中的两个事务所执行的最后一条 ...
- swift 颜色设置方法
如下: func RGB(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor { return UIColor.init(red: r ...
- LeetCode-111.Mininum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- python-面向对象-01_面向对象(OOP)基本概念
面向对象(OOP)基本概念 面向对象编程 —— Object Oriented Programming 简写 OOP 目标 了解 面向对象 基本概念 01. 面向对象基本概念 我们之前学习的编程方式就 ...