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 ...
随机推荐
- HTMLFormElement获取表单里面所有的值然后以json形式返回
function HTMLFormElement(){ this.init(); return this.json; } HTMLFormElement.prototype.init = functi ...
- STM32平台SD卡的FatFS文件系统开发
STM32平台SD卡的FatFS文件系统开发 系统平台: STM32系列的STM32F103ZE SPI方式与SD卡通信 SD上移植FatFS系统 1 FatFS文件系统 1.1 FatFS简介 Fa ...
- 轻量ORM-SqlRepoEx (十)SqlRepoEx Nuget包下载说明
ORM-SqlRepoEx 是 .Net平台下兼容.NET Standard 2.0,一个实现以Lambda表达式转转换标准SQL语句,使用强类型操作数据的轻量级ORM工具,在减少魔法字串同时,通过灵 ...
- 【oracle笔记2】约束
约束 *约束是添加在列上的,用来约束列的. 1. 主键约束(唯一标识) ***非空*** ***唯一*** ***被引用***(外键时引用主键) *当表的某一列被指定为主键后,该列就不能为空,不能有重 ...
- sql中UNION和UNION ALL的区别
写sql时我们经常会遇到需要把从多张表查询的集果集进行合并.这时就用到了union.使用union或union all 时一定要保证查询的列的一致性 .不然sql会报错.字段不一致的话可以用单引号来占 ...
- Linux Mysql 卸载
Linux下mysql的卸载: 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql 可以看到mysql的两个包: mysql-4.1.12-3.RHEL4.1 mysql ...
- HDU 5572--An Easy Physics Problem(射线和圆的交点)
An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- thinkphp 利用GD库在图片上写文字
<?php /** * Created by PhpStorm. * User: Administrator */ namespace Home\Event; use \Think\Image; ...
- Oracle批量删除表格数据
在开发阶段往Oracle数据库中多个表格中导入了许多测试数据,倘若一张张表执行"truncate table tablename"语句显得十分繁琐.在PL/SQL中可以用代码进行批 ...
- linux系统基础之-----磁盘结构(基于centos7.4 1708)