Unity3D让摄像机随player移动
来源:互联网 发布:淘宝鹰眼监控 编辑:程序博客网 时间:2024/05/29 03:25
代码很简单直接看:
using UnityEngine;using System.Collections;public class CameraController : MonoBehaviour { public GameObject player; private Vector3 offset;// Use this for initializationvoid Start () { offset = transform.position;}// Update is called once per framevoid Update () { transform.position = player.transform.position + offset;}}
把需要监听的GameObject添加到变量player中,就ok啦
Prefab(预制件):包含一个或一组物件的模板或蓝本,当修改其中一个物体的属性可以修改其他所有的物体属性。
Unity3D 控制物体移动、旋转、缩放
Transform基本移动函数:
1.指定方向移动:
//移动速度 float TranslateSpeed = 10f;//Vector3.forward 表示“向前”transform.Translate(Vector3.forward *TranslateSpeed);
2.全方向移动:
//x轴移动速度移动速度 float xSpeed = -5f;//z轴移动速度移动速度 float zSpeed = 10f;//向x轴移动xSpeed,同时想z轴移动zSpeed,y轴不动 transform.Translate(xSpeed,0,zSpeed);
3.重置坐标:
//x轴坐标 float xPostion = -5f;//z轴坐标 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;float x;float z;void Update () { if(Input.GetMouseButton(0)){//鼠标按着左键移动 y = Input.GetAxis("Mouse X") * Time.deltaTime * speed; x = Input.GetAxis("Mouse Y") * Time.deltaTime * speed; }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 = targetRotation; // 平滑旋转至目标角度 iszhuan = true; }}bool iszhuan= false;Quaternion targetRotation;void pinghuaxuanzhuan(){ if(iszhuan){ transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3); }}
键盘控制物体缩放:
float speed = 5.0f;float x;float z;void Update () { x = Input.GetAxis("Horizontal") * Time.deltaTime * speed; //水平 z = Input.GetAxis("Vertical") * Time.deltaTime * speed; //垂直//"Fire1","Fine2","Fine3"映射到Ctrl,Alt,Cmd键和鼠标的三键或腰杆按钮。新的输入轴可以在Input Manager中添加。 transform.localScale += new Vector3(x, 0, z); /**---------------重新设置角度(一步到位)----------------**/ //transform.localScale = new Vector3(x, 0, z);}
0 0
- Unity3D让摄像机随player移动
- Unity3D 人物移动 摄像机跟随
- unity3d移动摄像机,支持PC、手机
- Unity3d摄像机跟随角色移动脚本
- Unity3D开发之控制摄像机移动
- Unity3D始终让物体面向主摄像机
- Unity3D Player角色移动控制脚本
- Unity3D Player角色移动控制脚本
- 摄像机随鼠标移动
- unity3d摄像机
- unity3d摄像机
- unity3d摄像机
- unity3d摄像机
- Unity3D 摄像机
- unity3d--摄像机
- unity3d摄像机
- unity3d摄像机
- 深入浅出游戏算法(4)-unity3d算法(3)-移动摄像机
- MacPro 利用SCP上传下载Linux服务器文件
- WindowManager浅析
- 将微信小视频发送给QQ好友
- MYSQL explain详解
- 屌丝、小白怎么拿国内巨头offer
- Unity3D让摄像机随player移动
- 庖丁解牛-----winpcap源码彻底解密
- ROS错误 rospkg.common.ResourceNotFound: kdl
- Lua和C++交互详细总结
- linux 关于文件下载,解压
- Android adb命令整理
- spring @Autowired 解释
- SQLite增删改查
- css3-盒子模型之边框