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 ...
随机推荐
- mybatics之trim
1.<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=&quo ...
- 关于swing界面label和button的动态设置文字
在引入发送验证码功能后,想让button的文本动态变化,发现如下方法并不能做到: int limitSec=10; while(limitSec>0){ sendyzhm.setEnabled( ...
- linux 重启 启动 apache服务
如何使用service httpd restart,不成功的话,直接去apache文件目录里去找可执行文件,执行启动. 一般apache目录 ./usr/local/httpd/bin ,在bin目 ...
- 课时49.非input标签(掌握)
1.select标签(下拉列表) 什么是下拉列表? 这就是下拉列表 作用: 用于定义下拉列表 格式: <select> <option>列表数据</option> ...
- 创建oracle数据库表空间并分配用户
我们在本地的oracle上或者virtualbox的oracle上 创建新的数据库表空间操作:通过system账号来创建并授权/*--创建表空间create tablespace YUJKDATAda ...
- iOS:网络请求(17-12-26更)
先开一篇,以后再补充... 1.判断请求数据是否为空. 参考简书<解决NSNull对象的烦恼> --ma772528138 之前使用网络请求没丢包,而使用 socket 的时候丢过包.如果 ...
- Error creating bean with name 'mapper' defined in class path resource [applicationcontext.xml]: Cannot resolve reference to bean 'factory' while setting bean property 'sqlSessionFactory'; nested excep
Error creating bean with name 'mapper' defined in class path resource [applicationcontext.xml]: Cann ...
- 【若泽大数据实战第二天】Linux命令基础
Linux基本命令: 查看IP: ifconfig 或者 hostname -i(需要配置文件之后才可以使用) ipconfig(Windows) 关闭防火墙: Service iptables st ...
- Xcode9.2 添加iOS11.2以下旧版本模拟器
问题起源 由于手边项目需要适配到iOS7, 但是手边的测试机都被更新到最新版本,所以有些潜在的bug,更不发现不了.最近就是有个用户提出一个bug,而且是致命的,app直接闪退.app闪退,最常见的无 ...
- react组件间传值详解
一.父子组件间传值 <1>父传子 父组件: