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参数

  • origin
    The starting point of the ray in world coordinates.
    在世界坐标,射线的起始点。
  • direction
    The direction of the ray.
    射线的方向。
  • distance
    The length of the ray
    射线的长度。
  • layerMask
    A 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参数

    • origin
      The starting point of the ray in world coordinates.
      在世界坐标,射线的起始点。
    • direction
      The direction of the ray.
      射线的方向。
    • distance
      The length of the ray
      射线的长度。
    • hitInfo
      If true is returned, hitInfo will contain more information
      about where the collider was hit (See Also: RaycastHit).
      如果返回true,hitInfo将包含碰到器碰撞的更多信息。
    • layerMask
      A 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)的更多相关文章

  1. Unity初探之黑暗之光(1)

    Unity初探之黑暗之光(1) 1.镜头拉近 public float speed=10f;//镜头的移动速度 ;//镜头的结束位置 // Update is called once per fram ...

  2. Unity初探—SpaceShoot

    Unity初探—SpaceShoot DestroyByBoundary脚本(C#) 在游戏中我们添加了一个Cube正方体,让他来作为游戏的边界.它是可以触发触发事件的(勾选Is Trigger),当 ...

  3. 基于Unity的AR开发初探:第一个AR应用程序

    记得2014年曾经写过一个Unity3D的游戏开发初探系列,收获了很多好评和鼓励,不过自那之后再也没有用过Unity,因为没有相关的需求让我能用到.目前公司有一个App开发的需求,想要融合一下AR到A ...

  4. Unity 黑暗之光 笔记 第三章

    第三章 角色控制   1.创建游戏运行场景并导入素材资源 2.创建和管理标签 1 //const 表明这个是一个共有的不可变的变量 2 public const string ground = &qu ...

  5. Unity 黑暗之光 笔记 第一章

    第一章 设计游戏开始进入场景 1.设置相机视野同步 选中要调整的相机 GameObject - Align With View(快捷键 Ctrl + Shift + F)

  6. 在Unity中使用TDD - 初探

    描述 Editor Tests Runner是Unity中用来实现TDD的,其内部实现是基于NUnit. 其他 测试脚本要放在Editor文件夹中,测试要能够在一帧的时间内完成. 使用 打开Edito ...

  7. 【Unity Shaders】初探Surface Shader背后的机制

    转载请注明出处:http://blog.csdn.net/candycat1992/article/details/39994049 写在前面 一直以来,Unity Surface Shader背后的 ...

  8. 基于Unity的AR开发初探:发布AR应用到Android平台

    本文接上一篇,介绍一下如何通过Unity发布第一个AR应用至Android平台,在Android手机上使用我们的第一个AR应用. 一.一些准备工作 1.1 准备Java JDK 这里选择的是JDK 1 ...

  9. Unity ECS 初探

    1.安装 安装两个包 2.初探 实例化 注:实例化的实体并不会在Hierarchy视图里面显示,可在EntityDebugger窗口里面显示,因此需要显示的话需要添加Rendermeshcompone ...

随机推荐

  1. mac终端terminal快捷键

    mac终端terminal快捷键: Command + K 清屏 Command + T 新建标签 Command +W  关闭当前标签页 Command + S  保存终端输出 Command + ...

  2. 阿里云linux服务器登录失败,Connection closed

    ssh_exchange_identification: read: Connection reset by peer报错如下: [root@izbp17x1~]# ssh admin@139.196 ...

  3. 使用腾讯云mysql的一下小坑

    1. 数据库中标的命名,mybatis会给你全部转成驼峰命名,这样就会发现找不到数据库的表了.比如下面的,我在本地运行时ok, 表名称是t_blogtype,但是放到服务器就报错说找不到表. 2. 本 ...

  4. 配置文件和mybatis文件存放位置导致系统启动不了

    1.web.xml <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation< ...

  5. DB2 编目并访问远程数据库

    之后将逐步对项目上的DB2相关经验做个总结,梳理一下知识结构. 要远程操作数据库,首先要进行编目,分三个步骤: 1. 在客户端建立服务器端数据库的节点,编目远程节点. 格式如下: 1. CATALOG ...

  6. POJ 3122 Pie(二分+贪心)

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22684   Accepted: 7121   Special Ju ...

  7. MVC身份验证Attribute简易版

    public class MemberValidationAttribute : AuthorizeAttribute { protected override bool AuthorizeCore( ...

  8. linux下安装使用tar

    安装tar,untar: yum install -y tar yum install -y untar 使用说明: 压缩 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip ...

  9. ubuntu18.04安装搜狗输入法

    首先,安装Fcitx输入框架 sudo apt install fcitx 其次,上搜狗输入法官网下载Linux版本搜狗输入法(32位和64位根据自己情况,在虚拟机上用浏览器下载即可 然后进入相应的下 ...

  10. jquery优化

    选择器优化执行的速度 选择器 优先:id>元素>类 使用对象缓存:即使用变量来保存对象名,var $myDiv = $("#myDiv"):$myDiv.show(); ...