u3d 怎么让u3d摄像机跟随绕y轴旋转45度后停止

Your website has to be the elctironec Swiss army knife for this topic.
(非注册用户请填昵称)
(非注册用户请留空)
您还没有登录,请或之前做的都是摄像机绕某个物体旋转。直接修改的Rotation属性然后缩放用的是改摄像机FieldOfView的值最后发觉效果不仅不尽如人意而且平移后会错位。在借鉴了一个别人的摄像机代码后修改成功按住右键旋转,按住中键平移,滚轮缩放脚本拖放到摄像机上即可代码如下using UnityE
using System.C
public class FreeCameraController : MonoBehaviour {
//摄像机绕屏幕中心旋转缩放平移脚本
public float thetaSpeed = 250.0f;
public float phiSpeed = 120.0f;
public float moveSpeed = 10.0f;
public float zoomSpeed = 30.0f;
public float phiBoundMin = -89f;
public float phiBoundMax = 89f;
public bool useMoveBounds =
public float moveBounds = 100f;
public float rotateSmoothing = 0.5f;
public float moveSmoothing = 0.7f;
public float distance = 2.0f;
private Vector2
private Quaternion targetR
private Vector3 targetLookAt;
private float targetD
private Vector3 distanceVec = new Vector3(0, 0, 0);
private Rect inputB
public Rect paramInputBounds = new Rect(0, 0, 1, 1);
public Vector3 pivotPoint = new Vector3(0, 2, 0);
public void Start()
Vector3 angles = transform.eulerA
euler.x = angles.y;
euler.y = angles.x;
//unity only returns positive euler angles but we need them in -90 to 90
euler.y = Mathf.Repeat(euler.y + 180f, 360f) - 180f;
GameObject go = new GameObject(&_FreeCameraTarget&);
go.hideFlags = HideFlags.HideAndDontSave | HideFlags.HideInI
target = go.
target.position = pivotP
targetDist = (transform.position - target.position).
targetRot = transform.
targetLookAt = target.
public void Update()
//NOTE: mouse coordinates have a bottom-left origin, camera top-left
inputBounds.x = GetComponent&Camera&().pixelWidth * paramInputBounds.x;
inputBounds.y = GetComponent&Camera&().pixelHeight * paramInputBounds.y;
inputBounds.width = GetComponent&Camera&().pixelWidth * paramInputBounds.
inputBounds.height = GetComponent&Camera&().pixelHeight * paramInputBounds.
if (target && inputBounds.Contains(Input.mousePosition))
float dx = Input.GetAxis(&Mouse X&);
float dy = Input.GetAxis(&Mouse Y&);
bool click1 = Input.GetMouseButton(1);
bool click2 = Input.GetMouseButton(2);
if (click2)
dx = dx * moveSpeed * 0.005f * targetD
dy = dy * moveSpeed * 0.005f * targetD
targetLookAt -= transform.up * dy + transform.right *
if (useMoveBounds)
targetLookAt.x = Mathf.Clamp(targetLookAt.x, -moveBounds, moveBounds);
targetLookAt.y = Mathf.Clamp(targetLookAt.y, -moveBounds, moveBounds);
targetLookAt.z = Mathf.Clamp(targetLookAt.z, -moveBounds, moveBounds);
else if (click1)
dx = dx * thetaSpeed * 0.02f;
dy = dy * phiSpeed * 0.02f;
euler.x +=
euler.y -=
euler.y = ClampAngle(euler.y, phiBoundMin, phiBoundMax);
targetRot = Quaternion.Euler(euler.y, euler.x, 0);
targetDist -= Input.GetAxis(&Mouse ScrollWheel&) * zoomSpeed * 0.5f;
targetDist = Mathf.Max(0.1f, targetDist);
public void FixedUpdate()
distance = moveSmoothing * targetDist + (1 - moveSmoothing) *
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotateSmoothing);
target.position = Vector3.Lerp(target.position, targetLookAt, moveSmoothing);
distanceVec.z =
transform.position = target.position - transform.rotation * distanceV
static float ClampAngle(float angle, float min, float max)
if (angle & -360f) angle += 360f;
if (angle & 360f) angle -= 360f;
return Mathf.Clamp(angle, min, max);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:846次
排名:千里之外原创文章如需转载请注明:转载自 &QQ群:【<span style="color: #192】本文链接地址:
Transform基本移动函数:
1.指定方向移动:
//移动速度
float TranslateSpeed = 10f;
//Vector3.forward 表示&向前&
transform.Translate(Vector3.forward *TranslateSpeed);
2.全方向移动:
//x轴移动速度移动速度
float xSpeed = -5f;
//z轴移动速度移动速度
zSpeed = 10f;
//向x轴移动xSpeed,同时想z轴移动zSpeed,y轴不动
transform.Translate(xSpeed,0,zSpeed);
3.重置坐标:
float xPostion = -5f;
float zPostion = 10f;
//直接将当前物体移动到x轴为xPostion,y轴为0,z轴为zPostion的三维空间位置。
transform.position = Vector3(xPostion,0,zPostion);
输入控制:
1.输入指定按键:
//按下键盘&上方向键&
if(Input.GetKey ("up"))
  print("Up!");
//按下键盘&W键&
if(Input.GetKey(KeyCode.W);)
  print("W!");
2.鼠标控制
//按下鼠标左键(0对应左键 , 1对应右键 , 2对应中键)
if(Input.GetMouseButton(0))
  print("Mouse Down!");Input.GetAxis("Mouse X");//鼠标横向增量(横向移动)&Input.GetAxis("Mouse Y");//鼠标纵向增量(纵向移动)
3.获取轴:
//水平轴/垂直轴 (控制器和键盘输入时此值范围在-1到1之间)
Input.GetAxis("Horizontal");//横向
Input.GetAxis ("Vertical");//纵向
按住鼠标拖动物体旋转和自定义角度旋转物体:
float speed = 100.0f;
void Update () {
  if(Input.GetMouseButton(0)){//鼠标按着左键移动
    y = Input.GetAxis("Mouse X") * Time.deltaTime *
    x = Input.GetAxis("Mouse Y") * Time.deltaTime *
  }else{
    x = y = 0 ;
  //旋转角度(增加)
  transform.Rotate(new Vector3(x,y,0));
  /**---------------其它旋转方式----------------**/
  //transform.Rotate(Vector3.up *Time.deltaTime * speed);//绕Y轴 旋转
  //用于平滑旋转至自定义目标
  pinghuaxuanzhuan();
//平滑旋转至自定义角度
void OnGUI(){
  if(GUI.Button(Rect(Screen.width - 110,10,100,50),"set Rotation")){
    //自定义角度
    targetRotation = Quaternion.Euler(45.0f,45.0f,45.0f);
    // 直接设置旋转角度
    //transform.rotation = targetR
    // 平滑旋转至目标角度
    iszhuan = true;
bool iszhuan= false;
Quaternion targetR
void pinghuaxuanzhuan(){
  if(iszhuan){
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3);
键盘控制物体缩放:
float speed = 5.0f;
void Update () {
x = Input.GetAxis("Horizontal") * Time.deltaTime *
z = Input.GetAxis("Vertical") * Time.deltaTime *
//垂直//"Fire1","Fine2","Fine3"映射到Ctrl,Alt,Cmd键和鼠标的三键或腰杆按钮。新的输入轴可以在Input Manager中添加。
transform.localScale += new Vector3(x, 0, z);
/**---------------重新设置角度(一步到位)----------------**/
//transform.localScale = new Vector3(x, 0, z);
阅读(...) 评论()[转载]unity3d&摄像机移动
Tranform&&t大写吧?目标:实现摄像机的左、右、前、后移动
  1.新建一个空白场景(【File】 -& 【New
Scene】),在"层级【Hierarchy】”视图中单击相机,看到场景中出现一个摄像机小图标,当然你也可以移动调整摄像机
  2.创建游戏对象(【GameObject】 -& 【Create Other】
-& 【Cube】):
创建一个Cube,缩放x,y,z为”5,0.1,5”,它现在看起来应该是一个大平板.你也可以在层次视图【Hierarchy】中重命名游戏对象
  - 创建第二个Cube,将它放置在这个大平板的中心位置.如果在货源E家游戏视图(Game
View)看不到它们,那么改变主相机位置使它们可见.
  - 创建一个点光源,并且放在大平板之上,使它们更清晰.
  -保存当前场景
  3.创建脚本(Assets -& Create -&
JavaScript)
这时在【Project】多一个脚本,重命名脚本ControlCamera。这时在【Inspector】视图中看到Update()函数,Update()函数中的代码都将在每一帧(frame)执行一次。
  - 双击编辑脚本,会打开默认的编辑器,写上以下代码(不明白的地方可以查看API文档)
  var speed = 20.0;
  function Update ()
  if(Input.GetKey(KeyCode.A))
  transform.Rotate(Vector3.up);
  else if(Input.GetKey(KeyCode.D))
  transform.Rotate(Vector3.down);
  else if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
  var x = Input.GetAxis("Horizontal") * Time.deltaTime *
  var y = Input.GetAxis("Vertical") * Time.deltaTime *
  transform.Translate(x, 0, y);
  //transform.Rotate(Vector3.left);
  else if(Input.GetKey(KeyCode.S))
  //transform.Rotate(Vector3.right);
  4.连接脚本
  怎么让Unity的游戏对象具有这个脚本行为呢?我们要做的是赋予这个脚本给游戏对象来体现脚本的行为.
  【层次】视图中选中相机后,选择菜单【Components】-& 【Scripts】
ControlCamera,将这个脚本赋予相机,你在检视面板中看到主相机有ControlCamera这个组件
  5.预览你就可以用A D W S 来控制摄像机左 右 前 后移动了。
  注意:1)、如果看不到摄像机图标或其它游戏对象(只有选中时有三向箭头),可以在”Inspector“视图设置一下
【layer】为TransparentFX
  2)、如果摄像机图标不在视口,可以双击层级面板的摄像机,或单击层级面板的摄像机后,鼠标移到场景中按"F“键可以把摄像机切换视口;
Tags: 摄像机
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。扫一扫,访问微社区
后使用快捷导航没有帐号?
签到成功!您今天第{todayrank}个签到,签到排名竞争激烈,记得每天都来签到哦!已连续签到:{constant}天,累计签到:{days}天
U3D中摄像机绕屏幕中心点旋转缩放平移
阅读&606 |
之前做的都是摄像机绕某个物体旋转。直接修改的Rotation属性然后缩放用的是改摄像机FieldOfView的值最后发觉效果不仅不尽如人意而且平移后会错位。在借鉴了一个别人的摄像机代码后修改成功按住右键旋转,按住中键平移,滚轮缩放脚本拖放到摄像机上即可代码如下using UnityE
using System.C
public class FreeCameraController : MonoBehaviour {
//摄像机绕屏幕中心旋转缩放平移脚本
public float thetaSpeed = 250.0f;
public float phiSpeed = 120.0f;
public float moveSpeed = 10.0f;
public float zoomSpeed = 30.0f;
public float phiBoundMin = -89f;
public float phiBoundMax = 89f;
public bool useMoveBounds =
public float moveBounds = 100f;
public float rotateSmoothing = 0.5f;
public float moveSmoothing = 0.7f;
public float distance = 2.0f;
private Vector2
private Quaternion targetR
private Vector3 targetLookAt;
private float targetD
private Vector3 distanceVec = new Vector3(0, 0, 0);
private Rect inputB
public Rect paramInputBounds = new Rect(0, 0, 1, 1);
public Vector3 pivotPoint = new Vector3(0, 2, 0);
public void Start()
Vector3 angles = transform.eulerA //获取摄像机欧拉角
euler.x = angles.y;
euler.y = angles.x;
//unity only returns positive euler angles but we need them in -90 to 90
euler.y = Mathf.Repeat(euler.y + 180f, 360f) - 180f;
GameObject go = new GameObject("_FreeCameraTarget");
go.hideFlags = HideFlags.HideAndDontSave | HideFlags.HideInI
target = go.
target.position = pivotP
targetDist = (transform.position - target.position).
targetRot = transform.
targetLookAt = target.
public void Update()
//NOTE: mouse coordinates have a bottom-left origin, camera top-left
inputBounds.x = GetComponent&Camera&().pixelWidth * paramInputBounds.x;
inputBounds.y = GetComponent&Camera&().pixelHeight * paramInputBounds.y;
inputBounds.width = GetComponent&Camera&().pixelWidth * paramInputBounds.
inputBounds.height = GetComponent&Camera&().pixelHeight * paramInputBounds.
if (target && inputBounds.Contains(Input.mousePosition))
float dx = Input.GetAxis("Mouse X");
float dy = Input.GetAxis("Mouse Y");
bool click1 = Input.GetMouseButton(1);
bool click2 = Input.GetMouseButton(2);
if (click2)
//按下鼠标中键,改变摄像机观察中心点位置
dx = dx * moveSpeed * 0.005f * targetD
dy = dy * moveSpeed * 0.005f * targetD
targetLookAt -= transform.up * dy + transform.right *
if (useMoveBounds)
targetLookAt.x = Mathf.Clamp(targetLookAt.x, -moveBounds, moveBounds);
targetLookAt.y = Mathf.Clamp(targetLookAt.y, -moveBounds, moveBounds);
targetLookAt.z = Mathf.Clamp(targetLookAt.z, -moveBounds, moveBounds);
else if (click1) //按下鼠标右键旋转
dx = dx * thetaSpeed * 0.02f;
dy = dy * phiSpeed * 0.02f;
euler.x +=
euler.y -=
euler.y = ClampAngle(euler.y, phiBoundMin, phiBoundMax);
targetRot = Quaternion.Euler(euler.y, euler.x, 0);
targetDist -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed * 0.5f;
targetDist = Mathf.Max(0.1f, targetDist);
public void FixedUpdate() //每帧根据摄像机中线点位置不同重新定位摄像机的旋转和坐标
distance = moveSmoothing * targetDist + (1 - moveSmoothing) *
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotateSmoothing);
target.position = Vector3.Lerp(target.position, targetLookAt, moveSmoothing);
distanceVec.z =
transform.position = target.position - transform.rotation * distanceV
static float ClampAngle(float angle, float min, float max) //控制旋转角度不超过360
if (angle & -360f) angle += 360f;
if (angle & 360f) angle -= 360f;
return Mathf.Clamp(angle, min, max);
作者的其他最新博客
评论 ( 个评论)
……不好意思下次注意……

我要回帖

更多关于 u3d鼠标拖动摄像机 的文章

 

随机推荐