unity初探之黑暗之光(2)
unity初探之黑暗之光(2)
一、设置角色跟随鼠标点击移动
思路:使用charactercollider的SimpleMove方法来控制角色的移动。通过摄像机的射线投射到地面,通过屏幕上的一个点也就是鼠标单击的点。该射线与地面发生碰撞返回发生碰撞的点,然后让角色转向该点,开始移动。当移动到一定范围时停止移动。
使用到的方法:
Camera.ScreenPointToRay 屏幕位置转射线
function ScreenPointToRay (position : Vector3) : Ray
Description描述
Returns a ray going from camera through a screen point.
返回一条射线从摄像机通过一个屏幕点。
Resulting ray is in world space, starting on the near plane of the camera and going through position's (x,y) pixel coordinates on the screen (position.z is ignored).
产生的射线是在世界空间中,从相机的近裁剪面开始并穿过屏幕position(x,y)像素坐标(position.z被忽略)。
Screenspace is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight).
屏幕空间以像素定义。屏幕的左下为(0,0);右上是(pixelWidth,pixelHeight)。
Physics.Raycast 光线投射
static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters参数
- originThe starting point of the ray in world coordinates.
在世界坐标,射线的起始点。 - directionThe direction of the ray.
射线的方向。 - distanceThe length of the ray
射线的长度。 - layerMaskA Layer mask that is used to selectively ignore colliders
when casting a ray.
只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
• static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters参数
- originThe starting point of the ray in world coordinates.
在世界坐标,射线的起始点。 - directionThe direction of the ray.
射线的方向。 - distanceThe length of the ray
射线的长度。 - hitInfoIf true is returned, hitInfo will contain more information
about where the collider was hit (See Also: RaycastHit).
如果返回true,hitInfo将包含碰到器碰撞的更多信息。 - layerMaskA Layer mask that is used to selectively ignore colliders
when casting a ray.
只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
Transform.LookAt 注视
function LookAt (target : Transform, worldUp : Vector3 = Vector3.up) : void
Description描述
Rotates the transform so the forward vector points at /target/'s current position.
旋转物体,这样向前向量指向target的当前位置。简单说,
旋转物体使z轴指向目标物体。
当该物体设置了LookAt并指定了目标物体时,该物体的z轴将始终指向目标物体,在设置了worldUp轴向时,该物体在更接近指定的轴向是旋转便的灵活,注意worldUp指的是世界空间,不论你物体在什么位置,只要接近指定的轴方向,旋转会变的更灵活。
二、绕物相机旋转,拉近拉远
public float maxDis = 20f;
public float minDis = 2f;
public int scrollSpeed = ;
public float distance=;
public int rotateSpeed = ; private Transform player;
private Vector3 offSet;
private bool isRotate = false;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag(Tags.Player).transform;
transform.LookAt(player);
offSet = transform.position - player.position;//偏位
} // Update is called once per frame
void Update () {
transform.position = player.position + offSet; Rotate();
scroll();
}
//右键控制相机围绕对象旋转
void Rotate()
{
if (Input.GetMouseButtonDown())
{
isRotate = true;
}
if (Input.GetMouseButtonUp())
{
isRotate = false;
} if (isRotate)
{
Vector3 originalPosion = transform.position;
Quaternion originalRotate = transform.rotation; transform.RotateAround(player.position, Vector3.up, rotateSpeed * Input.GetAxis("Mouse X")); print(rotateSpeed * Input.GetAxis("Mouse Y"));
transform.RotateAround(player.position, Vector3.right, rotateSpeed * Input.GetAxis("Mouse Y"));
float x = transform.eulerAngles.x;
if (x > ||x<)
{
transform.position = originalPosion;
transform.rotation = originalRotate;
}
}
offSet = transform.position - player.position;//重新得到相机与当前人物对象的向量
}
//滑轮控制镜头远近
void scroll()
{
distance = offSet.magnitude;
distance -= Input.GetAxis("Mouse ScrollWheel")*scrollSpeed;
distance = Mathf.Clamp(distance, minDis, maxDis);
offSet = offSet.normalized * distance;
}
使用到的方法:
Transform.RotateAround 围绕旋转
function RotateAround (point : Vector3, axis : Vector3, angle : float) : void
Description描述
Rotates the transform about axis passing through point in world coordinates by angle degrees.
按照angle度通过在世界坐标的point轴旋转物体。
简单的说,按照多少度在世界坐标的某位置轴旋转物体。
This modifies both the position and the rotation of the transform.
这个修改变换的位置和旋转角度。
Input.GetAxis("Mouse X")和Input.GetAxis("Mouse Y")可以分别得到鼠标在水平和垂直方向上拖动的动作信息。
Input.GetAxis("Mouse ScrollWheel")可以获得鼠标滚轮的前后滚动的动作信息。
Vector3.normalized 规范化
var normalized : Vector3
Description描述
Returns this vector with a magnitude of 1 (Read Only).
返回向量的长度为1(只读)。
When normalized, a vector keeps the same direction but its length is 1.0.
当规格化后,向量保持同样的方向,但是长度变为1.0。
Note that the current vector is unchanged and a new normalized vector is returned. If you want to normalize the current vector, use Normalize function.
注意,当前向量是不改变的并且返回一个新的规范化的向量。如果你想规范化当前向量,使用Normalize函数。
If the vector is too small to be normalized a zero vector will be returned.
如果这个向量太小而不能被规范化,一个零向量将会被返回。
unity初探之黑暗之光(2)的更多相关文章
- Unity初探之黑暗之光(1)
Unity初探之黑暗之光(1) 1.镜头拉近 public float speed=10f;//镜头的移动速度 ;//镜头的结束位置 // Update is called once per fram ...
- Unity初探—SpaceShoot
Unity初探—SpaceShoot DestroyByBoundary脚本(C#) 在游戏中我们添加了一个Cube正方体,让他来作为游戏的边界.它是可以触发触发事件的(勾选Is Trigger),当 ...
- 基于Unity的AR开发初探:第一个AR应用程序
记得2014年曾经写过一个Unity3D的游戏开发初探系列,收获了很多好评和鼓励,不过自那之后再也没有用过Unity,因为没有相关的需求让我能用到.目前公司有一个App开发的需求,想要融合一下AR到A ...
- Unity 黑暗之光 笔记 第三章
第三章 角色控制 1.创建游戏运行场景并导入素材资源 2.创建和管理标签 1 //const 表明这个是一个共有的不可变的变量 2 public const string ground = &qu ...
- Unity 黑暗之光 笔记 第一章
第一章 设计游戏开始进入场景 1.设置相机视野同步 选中要调整的相机 GameObject - Align With View(快捷键 Ctrl + Shift + F)
- 在Unity中使用TDD - 初探
描述 Editor Tests Runner是Unity中用来实现TDD的,其内部实现是基于NUnit. 其他 测试脚本要放在Editor文件夹中,测试要能够在一帧的时间内完成. 使用 打开Edito ...
- 【Unity Shaders】初探Surface Shader背后的机制
转载请注明出处:http://blog.csdn.net/candycat1992/article/details/39994049 写在前面 一直以来,Unity Surface Shader背后的 ...
- 基于Unity的AR开发初探:发布AR应用到Android平台
本文接上一篇,介绍一下如何通过Unity发布第一个AR应用至Android平台,在Android手机上使用我们的第一个AR应用. 一.一些准备工作 1.1 准备Java JDK 这里选择的是JDK 1 ...
- Unity ECS 初探
1.安装 安装两个包 2.初探 实例化 注:实例化的实体并不会在Hierarchy视图里面显示,可在EntityDebugger窗口里面显示,因此需要显示的话需要添加Rendermeshcompone ...
随机推荐
- [转]C#打造一个开源webgis(一)系统架构
搭建一个GIS系统,为了能同时适应C/S和B/S架构,建议是做成自己的地图服务api方式,这样,一个或多个系统,就能通过统一的地图服务接口提供,而通信可以采用http的resful方式,而一个webG ...
- iframe父页调子页和子页调父页方法
子页调用父页 window.parent.myChart.resize(); 父页调用子页 $("iframe")[0].contentWindow.myChart.resize( ...
- 使用cmd命令创建vue(ivieiw)项目
条件,安装好nodejs 第一步:先使用 vue create 命令创建一个项目,等待创建完成. 1.切换目录 2.创建项目 vue create [项目名称] 第二步:切换到项目中. 第三步:使用 ...
- 图片左右滚动控件(带倒影)——重写Gallery
转http://blog.csdn.net/ryantang03/article/details/8053643 今天在网上找了些资料,做了一个图片左右滚动的Demo,类似幻灯片播放,同时,图片带倒影 ...
- 转-四种方案解决ScrollView嵌套ListView问题
本人网上用的ID是泡面或安卓泡面,学习一年半之前开始从事Android应用开发,这是我写的第一篇Android技术文章,转载请注明出处和作者,有写的不好的地方还请帮忙指出,谢谢. 在工作中,曾多次碰到 ...
- 关于nodejs下载组件经常失败的问题
由于最近在刚开始做一个前台element和mybatisplus的项目,但是在使用nodejs下载vue的脚手架和各种组件时,会经常出现下载失败的问题,进而导致前台无法启动. 在网上查询之后发现在下载 ...
- easyui datagrid 异步加载数据时滚动条有时会自动滚到最底部的问题
在使用easyui 的datagrid异步加载数据时发现滚动条有时会自动滚到最底部.经测试发现,如果加载数据前没有选中行则不会出现这个问题.这样我们可以在重新异步加载数据前取消选中行就可以避免这个问题 ...
- 基于JQ的简版选项卡记录
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- json提取嵌套数据
//数据 string html = "{\"code\":\"0000\",\"desc\":\"\",\& ...
- mysql的docker化安装
mysql版本有很多,先看下各类版本号说明: 3.X至5.1.X:这是早期MySQL的版本.常见早期的版本有:4.1.7.5.0.56等. 5.4.X到5.7.X:这是为了整合MySQL AB公司社区 ...