Unity 相机平移、旋转、缩放

内容不多,一个脚本,直接上代码

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4
 5 public class Move : MonoBehaviour
 6 {
 7
 8     float speed = 20;
 9     public float distance_v;
10     public float distance_h;
11     public float rotation_H_speed = 1;
12     public float rotation_V_speed = 1;
13     public float max_up_angle = 80;              //越大,头抬得越高
14     public float max_down_angle = -60;           //越小,头抬得越低
15
16
17     private float current_rotation_H;  //水平旋转结果
18     private float current_rotation_V;  //垂直旋转结果
19     void LateUpdate()
20     {
21         // 旋转
22         if (Input.GetMouseButton(1))
23         {
24             //控制旋转
25             current_rotation_H += Input.GetAxis("Mouse X") * rotation_H_speed;
26             current_rotation_V += Input.GetAxis("Mouse Y") * rotation_V_speed;
27             //current_rotation_V = Mathf.Clamp(current_rotation_V, max_down_angle, max_up_angle);       //限制垂直旋转角度
28             transform.localEulerAngles = new Vector3(-current_rotation_V, current_rotation_H, 0f);
29             transform.Translate(Vector3.back * distance_h, Space.Self);
30             transform.Translate(Vector3.up * distance_v, Space.World);          //相对于世界坐标y轴向上
31         }
32
33         // 平移
34         if (Input.GetMouseButton(2))
35         {
36             this.transform.Translate(new Vector3(-Input.GetAxis("Mouse X") * rotation_H_speed, -Input.GetAxis("Mouse Y") * rotation_V_speed, 0f));
37         }
38     }
39     // Use this for initialization
40     void Start()
41     {
42
43     }
44
45     // Update is called once per frame
46     void Update()
47     {
48         // 移动
49         if (Input.GetKey(KeyCode.A)) //左移
50         {
51             transform.Translate(Vector3.left * speed * Time.deltaTime);
52         }
53         if (Input.GetKey(KeyCode.D)) //右移
54         {
55             transform.Translate(Vector3.right * speed * Time.deltaTime);
56
57         }
58         if (Input.GetKey(KeyCode.W)) //前移
59         {
60             transform.Translate(Vector3.forward * speed * Time.deltaTime);
61
62         }
63         if (Input.GetKey(KeyCode.S)) //后移
64         {
65             transform.Translate(Vector3.back * speed * Time.deltaTime);
66
67         }
68
69         // 缩放
70         if (Input.GetAxis("Mouse ScrollWheel") != 0)
71         {
72             //获取鼠标滚轮的滑动量
73             float wheel = Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 5000;
74
75             //改变相机的位置
76             this.transform.Translate(Vector3.forward * wheel);
77         }
78     }
79 }

Camera

原文地址:https://www.cnblogs.com/huojiaoqingchun0123/p/10888550.html

时间: 2024-08-04 08:03:07

Unity 相机平移、旋转、缩放的相关文章

iOS 开发----CGAffineTransform平移,旋转,缩放

在做平移,旋转,缩放的时候一定要记住三个步骤: 获取值(获取当前控件的 transform 属性值) 修改值(修改需要设置的 transform 属性值) 赋值   (将修改的 transform 属性的值赋给原来的值) /**   *  平移   *   *  @param transform 获取当前的形变 transform   *  @param tx        沿着 x 水平方向的平移   *  @param ty        沿着 y 垂直方向的平移   *   *  @ret

【安卓】自定义基于onDraw的任意动画(不仅仅是平移/旋转/缩放/alpha)、!

思路: 1.基于时间的显示映射.如:给定度数,显示圆弧,加上时序,即可有圆弧动画的效果 2.给定时序.用于驱动动画的一帧帧绘制 方案一.基于ObjectAnimator.动画运作时会调用degree对应set函数(基于放射调用),即setDegree. ObjectAnimator ani=ObjectAnimator.ofInt(myView, "degree", 0,300); ani.start(); 注:1>混编后,默认会将setDegree混掉,导致找不到函数,故混编后

osg中使用MatrixTransform来实现模型的平移/旋转/缩放

osg中使用MatrixTransform来实现模型的平移/旋转/缩放 转自:http://www.cnblogs.com/kekec/archive/2011/08/15/2139893.html#undefined MatrixTransform是从Transform - Group继承而来,因此可以在它的下面挂接Node对象. 通过设置其矩阵,来实现其下子节点的模型变换. -- 用局部坐标系来理解(局部坐标系又称惯性坐标系,其与模型的相对位置在变换的过程中始终不变) 如下代码: // 创建

osg矩阵变换节点-----平移旋转缩放

osg矩阵变换节点-----平移旋转缩放 转自:http://www.cnblogs.com/ylwn817/articles/1973396.html 平移旋转缩放这个三个是osg矩阵操作中,最常见的操作,下面给出示例以及说明 首先先了解下osg空间方向: osg方向如左图所示,x轴表示屏幕水平方向,y轴表示和屏幕垂直方向即屏幕里面方向,z轴表示屏幕垂直方向,每个箭头指向表示正方向 下面来学习矩阵变换操作 首先平移: #include<osgDB/ReadFile> #include<

Android bitmap的拷贝以及平移旋转缩放等效果

package com.example.bitmap; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; i

矩阵平移旋转缩放公式

(4)平移矩阵: 注:以上矩阵采用的行矩阵,向量采用的是行向量.

【安卓】自己定义基于onDraw的随意动画(不不过平移/旋转/缩放/alpha)、!

思路: 1.基于时间的显示映射.如:给定度数,显示圆弧,加上时序,就可以有圆弧动画的效果 2.给定时序. 用于驱动动画的一帧帧绘制 方案一.基于ObjectAnimator.动画运作时会调用degree相应set函数(基于放射调用),即setDegree. ObjectAnimator ani=ObjectAnimator.ofInt(myView, "degree", 0,300); ani.start(); 注:1>混编后,默认会将setDegree混掉,导致找不到函数,故混

【WebGL初学系列之五】旋转,平移,缩放

nbcoder.com地址:http://nbcoders.com/webgl-chu-xue-xi-lie-zhi-wu-ai.html 最近把WebGL做的相关Demo已经放在 http://www.nbcoders.com 上了,这样就可以直观的进行看效果. 地址:    http://lab.nbcoders.com/ixshells/ 本文旋转平移缩放Demo的地址: http://lab.nbcoders.com/ixshells/Html/ScaleRotateTranslate.

【Android】自定义ImageView实现图片的平移、缩放、旋转(手势操作)

http://blog.csdn.net/happy_bug/article/details/7895244 在网上搜到很多都是一样的版本,只有平移和缩放的功能.我在搜到的源代码基础上添加了旋转和边界检查的功能.