Unity学习笔记——利用脚本实现对一个物体的第三人称观察
来源:互联网 发布:久其软件 决算 编辑:程序博客网 时间:2024/05/16 01:45
首先,要为被观测对象设定一个子对象(用于添加组件和放入摄像机),然后再拖出(这样就能保持这个空物体的transform和被观测物体一致。),将摄像机拖入这个空物体之中作为子物体。其中是针对空物体的视角来进行旋转,针对它的子物体摄像机来进行缩放
脚本代码为
using UnityEngine;using System.Collections;public class FYLcamera : MonoBehaviour { public Transform target;//被观测物体 public Transform childCamera;//摄像机 public float rotateSpeed;//旋转速度 public float scaleSpeed;//缩放速度 public float minDistance;//最近缩放位置 public float maxDistance;//最远缩放位置 private float currentScale;//现有的缩放大小 private Vector3 currentRotation;//现有的旋转角 private Vector3 lastMousePosition;//记录鼠标上次的位置 // Use this for initialization void Start () { currentScale = childCamera.position.z;//初始化缩放大小 currentRotation = transform.eulerAngles;//初始化欧拉角 lastMousePosition = Input.mousePosition; } // Update is called once per frame void Update () { Vector3 mouseDelta = Input.mousePosition - lastMousePosition;//计算鼠标位移 lastMousePosition = Input.mousePosition; if (Input.GetMouseButton (1)) {//如果使用鼠标右键 currentRotation.x += mouseDelta.y * rotateSpeed * Time.deltaTime; currentRotation.y += mouseDelta.x * rotateSpeed * Time.deltaTime; //因为旋转方向和鼠标拖动方向正好相反,所以用以上公式计算旋转量 } currentScale += -Input.mouseScrollDelta.y * scaleSpeed * Time.deltaTime;//计算缩放值,因为我的摄像机是反的,所以要取负数 currentScale = Mathf.Clamp (currentScale, minDistance, maxDistance);//取闭区间,使得缩放值一直在规定的范围之内 transform.position = target.position;//空物体的坐标跟随被观测物体 transform.eulerAngles = currentRotation;//欧拉角设定为当前的旋转量 childCamera.localPosition = new Vector3 (0, 0, currentScale);//设定摄像机的缩放距离。 }}
0 0
- Unity学习笔记——利用脚本实现对一个物体的第三人称观察
- unity 学习 双击一个物体的实现
- unity中实现的第三人称摄像机控制脚本。
- Unity通过脚本实现给一个物体添加子物体
- Unity学习笔记——Unity基础一:unity界面、场景、游戏物体、组件等基本概念
- unity使用第三人称脚本时摄像头会晃动的问题
- Unity学习笔记 1st —— 物体会移动就是游戏的开始!
- Unity 3D鼠标操作实现旋转、缩放观察物体
- [Unity实战]相机观察物体
- unity学习日记:如何在手机上实现第三人称视角
- C#——实现物体的移动(unity)
- Unity实现第三人称角色控制
- unity学习之脚本,拾取物体,定时器
- 【Unity3D游戏开发学习笔记】(七)上帝之眼—第三人称摄像机的简单实现(跟随视角,自由视角)
- Unity——Rigidbody实现真实的物体旋转效果/解决物体添加刚体后不随父物体移动/hingejoint
- OSG学习笔记11 物体控制—对点选物体进行平移、缩放、旋转
- 【Unity 3D】学习笔记四十七:实例——观察模型
- Unity第一人称和第三人称视角脚本
- System.out.println内容写入txt
- linux命令详解
- A different object with the same identifier value was already associated with the session
- android 解决EditText无法失去焦点和失去焦点后隐藏软键盘的问题
- 含重复元素的全排列
- Unity学习笔记——利用脚本实现对一个物体的第三人称观察
- Unity基于NGUI实现拖拽功能
- 8. Smarty 基本语法
- Android 5.0之后 点击 悬浮穿回到顶部。
- Android中SQLite的使用
- [BZOJ3571][HNOI2014]画框
- POJ 2828 Buy Tickets(线段树—查找并更新从左到右的第i个1)
- fms服务端语法
- Android Hanlder综合