Unity相机围绕物体

using DG.Tweening;
using UnityEngine;

public class FollowTrackingCamera : MonoBehaviour
{
public static FollowTrackingCamera _Instance;

// Camera target to look at.
//相机跟随目标
public Transform target;

//相机旋转角度
public Vector3 CameraRotation;

// Exposed vars for the camera position from the target.
//从目标到摄像机位置的外露vars.
public float height = 20f;
public float distance = 20f;

// Camera limits.
//相机移动范围
public float min = 10f;
public float max = 60;

// Options.
//public bool doRotate;
//相机旋转以及缩放功能开关
public bool doZoom;
public bool doRotate;

// The movement amount when zooming.缩放时的移动量。
public float zoomStep = 30f;
public float zoomSpeed = 5f;
private float heightWanted;
private float distanceWanted;

public float xSpeed = 3.0f;
public float ySpeed = 3.0f;

public float yMinLimit = -20f;
public float yMaxLimit = 80f;

//public float xMinLimit = 30f;
//public float xMaxLimit = 220f;

public float distanceMin = 1.5f;
public float distanceMax = 15f;

public float smoothTime = 2f;

float rotationYAxis = 230.0f;
float rotationXAxis = -8.0f;

float velocityX = 0.0f;
float velocityY = 0.0f;

//两根手指
private Touch oldTouch1;
private Touch oldTouch2;
//Vector2 m_screenPos = Vector2.zero; //记录手指触碰的位置

float scaleFactor;

// Result vectors.
private Vector3 zoomResult;//缩放后坐标
private Quaternion rotationResult;
private Vector3 targetAdjustedPosition;
private Quaternion rotation;

private void Awake()
{
_Instance = this;
}

void Start()
{

init();

}
void init()
{
Position = transform.position;
rotation = transform.rotation;

//得到相机欧拉角
Vector3 angles = transform.eulerAngles;
//相机绕Y轴转动的角度值
rotationYAxis = angles.y;
//相机绕X轴转动的角度值
rotationXAxis = angles.x;
print("相机初始位置" + rotationXAxis);
//print("Y轴数值"+ rotationYAxis);
//print("X轴数值" + rotationXAxis);

// Initialise default zoom vals.
//相机当前高度赋值于目标高度
heightWanted = height;
distanceWanted = distance;
// Setup our default camera. We set the zoom result to be our default position.
zoomResult = new Vector3(0f, height, -distance);
}
public static float InitAngle=-90;
public float CurrAngle=45;
public float WantedScale = 20;//想要的缩进大小
void LateUpdate()
{
if (IsInit == true)
{
distanceWanted = WantedScale;
AsianAni.Instance.Tween(InitAngle);
rotationXAxis = 25;
//DOTween.To(() => distanceWanted, x => distanceWanted = x, 19, 0.01f);
DOTween.To(() => rotationXAxis, x => rotationXAxis = x, CurrAngle, 0.5f);
rotationYAxis = 0;
IsInit = false;
}
// Check target.
//检测目标是否存在
if (!target)
{
Debug.LogError("This camera has no target, you need to assign a target in the inspector.");
return;
}

//相机视角缩放
if (doZoom)
{
//print(doRotate);
//if (Input.touchCount <= 0)
//{
// return;
//}
float mouseInput;
if (Input.touchCount > 1)
{

Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//第2点刚开始接触屏幕, 只记录,不做处理  
if (newTouch2.phase == TouchPhase.Began)
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
//return;
}
//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型  
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//两个距离只差,为正表示放大,为负表示缩小
float offset = newDistance - oldDistance;
//缩放因子

---------------------

原文地址:https://www.cnblogs.com/liyanyan665/p/11278070.html

时间: 2024-08-03 14:55:02

Unity相机围绕物体的相关文章

Unity 相机围绕物体指定观察角度和位置

问题:相机始终围绕着O观察,现在从A坐标和视角 运动旋转 到B坐标和视角.. 条件:abcd这个矩形盒差不多是物体O的顶视图尺寸,A点箭头是相机所在的起点和角度,B点为目标点需要观察的角度. 直接直线移动相机并按照AB夹角的差去移动和扭转相机的话 镜头很容易翻转!相机不可能总是在关注O对象!难受死了! 我能想到的最优解决办法: 水平围绕+position插值.angle插值!插值时使用Lerp函数,而非直接递增角度.递增位置! 我之所以最后这么"愉快"的决定实在是由于数学捉急,除非会构

unity 相机跟随物体(角色)

unity 相机平滑跟随游戏角色 把这个脚本赋给你的摄像机,再把游戏角色赋给character变量,之后就能实现摄像机平滑的跟随player在地球的任一角落了. using UnityEngine; using System.Collections; public class SmoothFollowerObj { private Vector3 targetPosition; private Vector3 position; private Vector3 velocity; private

[Unity实战]相机观察物体

在原有的旋转.缩放的基础上加上了"惯性". using UnityEngine; using System.Collections; public class CameraObserve : MonoBehaviour { public Transform targetObj; public float rotateSpeed = 100f; public float translateSpeed = 10f; public float minDistance = 3f;//缩放观察的

unity, 相机空间 与 相机gameObject的局部空间

在unity里 相机空间 与 相机gameObject的局部空间 不重合. Camera.worldToCameraMatrix的文档中有这样一句话: Note that camera space matches OpenGL convention: camera's forward is the negative Z axis. This is different from Unity's convention, where forward is the positive Z axis. 意思

如何让Maya中的相机和Unity相机的FOV(Angle of View)对上

问题: Unity中的Camera的FOV和Maya中Camera的Angle of View即使设置成相同值,预览时结果会有很大差异 原因: Unity Camera的FOV是Vertical FOV的角度 Maya Camera的Angle of View是Horizontal FOV的角度 解决方法 先保证Maya和Unity中的Aspect Ratio一致,然后再按照长宽比换算两者各自应该使用的FOV或者Angle of View. 例如按照下面的方式设置,即可实现两边完全一样 Unit

[Unity官方文档翻译]Primitive and Placeholder Objects Unity原生3D物体教程

Primitive and Placeholder Objects 原始的基础物体 Unity can work with 3D models of any shape that can be created with modelling software. However, there are also a number of primitive object types that can be created directly within Unity, namely the Cube, S

Unity中Instantiate物体失效问题

才开始学Unity,开始总是这样用Instantiate函数: GameObject temp = (GameObject)Instantiate(bulletSource, transform.position, transform.rotation); 然后该类中定义一个共有变量如下: public GameObject bulletSource; 在面板上先将预设体托到Hierarchy处,从该处再把对应的Object托到类的共有变量上. 开始这样是没有问题的,但是当该物体在某个时间销毁后

unity 相机的问题

屏幕无法同时显示UI 和场景 查了下 :是对 相机 设置没做好 Unity NGUI相机与主相机如何同时显示 分类: Unity3D2014-06-27 10:09 1260人阅读 评论(0) 收藏 举报 上一个小项目很容易就把ngui的相机和游戏的主相机结合起来,这次不知道怎么的,ui的东西始终看不到:经过一番查阅,发现针对相机而言有以下主要设定: 1 Layer,相机位于的层,想要同时显示几个相机,就让他们处于不同的层,通常只要UI层来放ngui的ui,其它的放默认 2 depth,这个属性

unity实现一个物体绕着某点旋转

transform.RotateAround(o.transform.position,Vector3.up,20*Time.deltaTime);transform.Rotate(Vector3.up, 20*Time.deltaTime, Space.World); RotateAround():让物体围绕招某点旋转,参数分别为,旋转的点坐标,旋转的方向,旋转的速度. Rotate():让物体自传,参数分别为:旋转的方向,旋转的速度,旋转的坐标系(世界坐标系和自身坐标系). 原文地址:htt