摄像机围绕人物旋转
2017-05-03 15:11
141 查看
用法:先在你的Player里新建一个空的Object重命名为CameraTarget(你起什么名字都行),然后将把脚本挂载到主摄像机上,将你新建的 CameraTarget拖动到脚本的Target属性,运行你就可以看到效果了using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class cc : MonoBehaviour{public bool HideAndShowCursor = true;public bool LockRotationWhenRightClick = false;public bool UseBlurEffect = true;public bool UseFogEffect = true;public Transform target;//要控制的对象public float targetHeight = 1.0f;public float distance = 5.0f;public float maxDistance = 20;public float minDistance = 0.6f;public float xSpeed = 250.0f;public float ySpeed = 120.0f;public int yMinLimit = -80;public int yMaxLimit = 80;public int zoomRate = 40;public float rotationDampening = 3.0f;public float zoomDampening = 10.0f;private float x = 0.0f;private float y = 0.0f;private float currentDistance;private float desiredDistance;private float correctedDistance;private bool grounded = false;void Start(){Screen.lockCursor = true;Vector3 angles = transform.eulerAngles;x = angles.x;y = angles.y;currentDistance = distance;desiredDistance = distance;correctedDistance = distance - 0.2f;// Make the rigid body not change rotationif (GetComponent<Rigidbody>())GetComponent<Rigidbody>().freezeRotation = true;}void LateUpdate(){// Don't do anything if target is not definedif (!target){GameObject go = GameObject.Find("Player");target = go.transform;transform.LookAt(target);return;}// If either mouse buttons are down, let the mouse govern camera positionif (LockRotationWhenRightClick == false){x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;}if (Input.GetMouseButton(0)){if (LockRotationWhenRightClick == false){x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;}}y = ClampAngle(y, yMinLimit, yMaxLimit);// set camera rotationQuaternion rotation = Quaternion.Euler(y, x, 0);// calculate the desired distancedesiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);correctedDistance = desiredDistance;// calculate desired camera positionVector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));// check for collision using the true target's desired registration point as set by user using heightRaycastHit collisionHit;Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);// if there was a collision, correct the camera position and calculate the corrected distancebool isCorrected = false;if (Physics.Linecast(trueTargetPosition, position, out collisionHit)){if (collisionHit.transform.name != target.name){position = collisionHit.point;correctedDistance = Vector3.Distance(trueTargetPosition, position);isCorrected = true;}}// For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistancecurrentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;// recalculate position based on the new currentDistanceposition = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight - 0.05f, 0));transform.rotation = rotation;transform.position = position;}private static float ClampAngle(float angle, float min, float max){if (angle < -360)angle += 360;if (angle > 360)angle -= 360;return Mathf.Clamp(angle, min, max);}}
相关文章推荐
- 摄像机围绕物体旋转观察
- Unity实现摄像机围绕物体旋转
- 控制摄像机围绕目标旋转观察
- 摄像机的旋转缩放与跟随,并且保持跟随的时候不受人物的影响。
- Unity3D【脚本】鼠标对摄像机的控制 - 点击鼠标,摄像机围绕圆心旋转(第三次修改)
- 鼠标控制摄像机围绕物体旋转拍摄
- unity 鼠标控制摄像机围绕物体旋转
- 摄像机围绕0.0.0点旋转并显示FPS值
- Unity3D【脚本】鼠标对摄像机的控制 - 点击鼠标,摄像机围绕圆心旋转
- Unity3D学习日记(五)实现围绕物品旋转的的摄像机控制
- Unity3D开发之控制摄像机围绕物体旋转 平移 缩放
- 【Unity 3D学习】获取鼠标滑动方向(上下左右)-控制摄像机围绕物体旋转
- 摄像机围绕0.0.0点旋转并显示FPS值
- 如何让摄像机跟随你的角色,并且按鼠标右键围绕旋转
- ogre代码实例-场景、摄像机的创建,节点的平移旋转缩放
- 摄像机旋转
- u3d 人物跟着鼠标旋转
- 用unity3d做一个物体围绕另外一个物体旋转。
- poj 1106(半圆围绕圆心旋转能够覆盖平面内最多的点)
- 图形学:图像围绕着某个点P(a,b)旋转------白话版