攝像機圍繞物體旋轉觀察
using UnityEngine;
public class CameraItem : MonoBehaviour {
//攝像機所要觀察的物體
public Transform target;
//攝像機距離物體的距離 這裡我們初始預設的是5
public float distance = 5f;
//攝像機相對於物體的旋轉速度
private float speedX = 240;
private float speedY = 120;
//限定攝像機只能旋轉的角度
private float minLimitY = -40f;
private float maxLimitY = 240f;
//這兩個值用於儲存攝像機自身的一個角度
private float mX = 0.0f;
private float mY = 0.0f;
//這兩個值用於限定攝像機距離物體的最大值和最小值
public float maxDistance = 2;
public float minDistance = 0.5f;
//滑鼠滾輪拉近鏡頭的速度
private float zoomSpeed = 2f;
public bool isNeedDamping = true;
public float damping = 2.5f;
void Start()
{
mX = transform.eulerAngles.x;
mY = transform.eulerAngles.y;
}
void LateUpdate()
{
if (target != null && Input.GetMouseButton(0))
{
mX += Input.GetAxis("Mouse X") * speedX * Time.deltaTime;
mY -= Input.GetAxis("Mouse Y") * speedY * Time.deltaTime;
mY = ClampAngle(mY, minLimitY, maxLimitY);
}
distance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
Quaternion mRotation = Quaternion.Euler(mY, mX, 0);
Vector3 mPosition = mRotation * new Vector3(0, 0, -distance) + target.position;
if (isNeedDamping)
{
transform.rotation = Quaternion.Lerp(transform.rotation, mRotation, Time.deltaTime * damping);
transform.position = Vector3.Lerp(transform.position, mPosition, Time.deltaTime * damping);
}
else
{
transform.rotation = mRotation;
transform.position = mPosition;
}
}
//限定角度
private float ClampAngle(float angle, float min, float max)
{
if (angle < -360) angle += 360;
if (angle > 360) angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
相關推薦
攝像機圍繞物體旋轉觀察
using System.Collections.Generic; using UnityEngine; public class CameraItem : MonoBehaviour { //攝像機所要觀察的物體 public Transform target; //攝像機距離物體的
unity 滑鼠控制攝像機圍繞物體旋轉
void LateUpdate() //對攝像機的操作寫在LateUpdate裡 { x += Input.GetAxis("Mouse X") * rotateSpeed
Three.js OrbitControls.js讓攝像機圍繞目標旋轉
例子 ... var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 ); var controls
unity 攝像機圍繞物體轉動(有萬向鎖)
public Transform target;//旋轉目標 public float distance = 1.8f;//攝像機和目標之間的距離 private float speedX = 240f;//x軸轉速 private float sp
unity攝像機環繞物體旋轉
最近做專案的時候遇到一個需求:需要環視某物體做360度展示 開始一直糾結,最終找到一個簡單的解決方法 實現原理: 把攝像機作為一個空物體的子物件,空物體自旋,攝像機也會旋轉,並且視角一直註釋著物體 具體實現: 要環視的物體 新建一個空物件
unity通過滑鼠操作攝像機圍繞主角縮放、旋轉、上下移動
控制攝像機的程式碼如下: using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 攝像機控制器. 將該指令碼掛載到Camera
Unity 2D 物體旋轉指向目標
Mathf.Atan2 反正切2 static function Atan2 (y : float, x : float) : float Description描述 Returns the angle in radians whose Tan is y/x. 以弧度為單位計
物體旋轉:Quaternion/eulerAngles四元數與尤拉角 u3d學習總結筆記本
目錄 1.獲取兩個物體的向量/角度 2.指定旋轉到角度 3.指向某個位置 4.自軸旋轉 5.繞軸旋轉 6.無旋轉 (這個物體完全對齊於世界或父軸) //========================================= 1.獲
Unity3D攝像機跟隨物體移動的程式碼控制
攝像機跟隨物體方法一是把攝像機設定為物體Player的子物體,給Player新增移動腳步就可以攝像機跟隨Player移動。移動的簡單腳步using UnityEngine; using System.
【OpenCV】OpenCV輪廓檢測,計算物體旋轉角度
OpenCV輪廓檢測,計算物體旋轉角度 效果還是有點問題的,希望大家共同探討一下 // FindRotation-angle.cpp : 定義控制檯應用程式的入口點。 // // findContours.cpp : 定義控制檯應用程式的入口點。 /
unity rotate 旋轉物體 限制物體旋轉角度 的大坑
今天可是遇到一個很簡單的需求,但是卻讓我蛋疼了半天。 滑動螢幕控制物體旋轉,但是旋轉的角度要在-60到60之間。 乍一聽這簡直是小兒科啊。 判斷一下角度不就行了。相比這四元數,尤拉角雖然有時會出現萬向
Unity中用觸控控制物體旋轉和放大
using UnityEngine; using System.Collections; using System.IO; public class ScaleAndRotate : MonoBehaviour { private Touch oldTouch1; /
unity 滑鼠拖動 物體旋轉 點選變色 拖動移動
public Camera camera; Ray ray; RaycastHit hitInfo; Vector3 offset; GameObject obj;//獲取點選到的物體 public GameObject tr
在threejs中對3D物體旋轉的思考
今天在寫threejs時,突然想到一個問題:一個3D物體需要旋轉時,一般情況下簡單的旋轉我都是使用尤拉角,稍微複雜一些的情況我會把尤拉角轉換成四元數進行旋轉(尤拉角複雜旋轉可能會產生的死鎖問題),但是在threejs中object3D的旋轉方法無論是使用se
ARFoundation - 實現物體旋轉, 平移,縮放
# ARFoundation - 實現物體旋轉, 平移,縮放 本文目的是為了確定在移動端怎樣通過單指滑動實現物體的旋轉,雙指實現平移和縮放。 前提知識: [ARFoundation - touch point座標點測試](https://www.cnblogs.com/grass-and-moon/p/
物體圍繞某個點旋轉一定角度
apply ring b+ can org lec wikipedia close sele 轉自:https://dawnarc.com/2016/06/ue4%E7%BA%BF%E6%80%A7%E4%BB%A3%E6%95%B0%E7%89%A9%E4%BD%93%E
EasyTouch物體的旋轉縮放
angle num prot auto cte locals isa form float public class ArMonoDevelop : MonoBehaviour { // Use this for initialization
canvas實現圖片圍繞左上角一點進行旋轉
canvas插入圖片,需要先在onload事件中預載入圖片,然後在用drawImage()方法插入圖片。 <!DOCTYPE html> <html> <head> <meta charset="utf-8">
判斷物體是否在攝像機視野中
void Start(){ Rect screenRect = new Rect(0, 0, Screen.width, Screen.Height); } void Update{ Vector3 screenPos = targetCamera.WorldToScreenP
Unity實現物體沿自身的任意軸向旋轉
一、建立一個需要旋轉的物體 二、編寫控制該物體的指令碼 using UnityEngine; using System.Collections; public class Test_ElectricFan : MonoBehaviour { public bool isOpen=