unity 控制对象移动、旋转
来源:互联网 发布:淘宝店铺贷款利息多少 编辑:程序博客网 时间:2024/06/10 02:37
- W/S :: 前进/后退
- A/D :: 下降/上升
- 鼠标滚轮 :: 对象移动的速度
- 光标水平移动 :: 对象左右方向旋转
- 光标上下移动 :: 对象上下方向旋转
using UnityEngine;using System.Collections;public class ObjectController : MonoBehaviour{ // x方向移动的速度 public float xSpeed = 1f; // y方向移动的速度 public float ySpeed = 1f; // z方向移动的速度 public float zSpeed = 1f; public Vector3 velocity = Vector3.zero; // 是否按下A键 private bool isAdown = false; // 是否按下D键 private bool isDdown = false; private float x = 0.0f; private float y = 0.0f; void Update() { float theZ = Input.GetAxis("Vertical") * this.zSpeed; float theX = Input.GetAxis("Horizontal") * this.xSpeed; // 设置位移 Vector3 v1 = new Vector3(0, 0, theZ); v1 = this.transform.TransformDirection(v1); v1 = v1 - Vector3.Dot(v1, Vector3.up) * Vector3.up; v1 = this.transform.InverseTransformDirection(v1); this.transform.Translate(v1); // 设置旋转角度 Vector3 v2 = new Vector3(0, theX, 0); this.transform.Rotate(v2); if (Input.GetKeyDown(KeyCode.A)) this.isAdown = true; else if (Input.GetKeyUp(KeyCode.A)) this.isAdown = false; else if (Input.GetKeyUp(KeyCode.D)) this.isDdown = false; else if (Input.GetKeyDown(KeyCode.D)) this.isDdown = true; if (this.isAdown) this.transform.position = Vector3.SmoothDamp(this.transform.position , this.transform.position - new Vector3(0, this.ySpeed, 0) , ref velocity, 0.1f); if (this.isDdown) this.transform.position = Vector3.SmoothDamp(this.transform.position , this.transform.position + new Vector3(0, this.ySpeed, 0) , ref velocity, 0.1f); float zoom = Input.GetAxis("Mouse ScrollWheel"); if (this.zSpeed + zoom < 10 && this.zSpeed + zoom > 0.1) this.zSpeed += zoom; } private void Rotate() { this.x += Input.GetAxis("Mouse X") * this.xSpeed; this.y -= Input.GetAxis("Mouse Y") * this.ySpeed; if (this.y < -360f) this.y += 360f; if (this.y > 360f) this.y -= 360f; y = Mathf.Clamp(y, -20f, 80f); Quaternion rotation = Quaternion.Euler(y * 0.5f, x, 0); this.transform.rotation = rotation; } private void LateUpdate() { Rotate(); }}
0 0
- unity 控制对象移动、旋转
- Unity控制移动、旋转
- unity 控制物体移动和旋转
- Unity 控制物体移动 旋转 缩放
- unity使用鼠标控制移动、缩放、旋转
- Unity 移动端实现pad 端acceleration 控制移动、旋转
- Unity 移动端简单手势控制(移动,旋转,缩放)
- unity中控制主角的移动和旋转
- Unity 3D:控制相机旋转、移动、缩放等功能
- unity 通过键盘控制角色的移动和旋转
- unity中控制物体的移动,和旋转
- 【unity实用技能】unity3d 陀螺仪控制camera移动旋转
- Unity鼠标控制物体360°旋转和点击移动
- unity FPS 将原来的left,right控制左右移动改为控制向左向右旋转
- Unity对象旋转功能
- unity 键盘控制移动
- Unity移动旋转比例缩放
- unity 移动端 缩放 旋转
- 二叉树的非递归遍历
- Handlebars 的使用
- KPI管理
- 响应式web设计
- swift简单学习之闭包(block)
- unity 控制对象移动、旋转
- H264 视频文件 帧格式 传输封装等
- 背包问题算法
- 初识java反射机制
- windows下安装redis
- Release编译出的ocx控件注册失败问题解决
- java设置环境变量CLASSPATH以及path环境变量的作用。
- 对iPhone手机型号进行判断
- web、ftp上传区别