-
C#教程之Unity实现人物旋转和移动效果
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
本文实例为大家分享了Unity实现人物旋转和移动的具体代码,供大家参考,具体内容如下
旋转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MouseLook : MonoBehaviour { public enum RotationAxes { MouseXandY = 0, MouseX = 1, MouseY = 2 } public RotationAxes axes = RotationAxes.MouseXandY; public float sensitivityHor = 9.0f; public float sensitivityVert = 9.0f; public float minVert = -45.0f; public float maxVert = 45.0f; private float _rotationX = 0; // Use this for initialization void Start () { Rigidbody body = GetComponent<Rigidbody> (); if (body != null ) { body.freezeRotation = true ; } } // Update is called once per frame void Update () { //水平旋转就是以Y轴作为旋转轴旋转,鼠标移动量为偏移量 if (axes == RotationAxes.MouseX) { transform.Rotate (0, Input.GetAxis( "Mouse X" ) * sensitivityHor, 0); //通过“增加”旋转角度进行旋转(X,Y,Z为对应方向的增加量),一般用于无限制旋转 } else if (axes == RotationAxes.MouseY) { _rotationX -= Input.GetAxis ( "Mouse Y" ) * sensitivityVert; _rotationX = Mathf.Clamp (_rotationX, minVert, maxVert); //限制_rotationX的值在minVert与minVert之间 float rotationY = transform.localEulerAngles.y; //Debug.Log ("rotationX:"+_rotationX+","+Input.GetAxis ("Mouse Y") * sensitivityVert); transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0); //直接“设置”旋转角度进行旋转,一般用于有限制旋转 } else { _rotationX -= Input.GetAxis ( "Mouse Y" ) * sensitivityVert; _rotationX = Mathf.Clamp (_rotationX, minVert, maxVert); float delta = Input.GetAxis ( "Mouse X" ) * sensitivityHor; float rotationY = transform.localEulerAngles.y + delta; transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0); } } } |
移动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent( typeof (CharacterController))] //如果对象没有该组件,则创建一个 [AddComponentMenu( "Control Script/FPS Input" )] //可以在Add Component查到 public class FPSInput : MonoBehaviour { private CharacterController _characterController; public float speed = 10.0f; public float gravity = -9.8f; // Use this for initialization void Start () { _characterController = GetComponent<CharacterController>(); //获取对象里的某一组件 } // Update is called once per frame void Update () { float deltaX = Input.GetAxis ( "Horizontal" ) * speed; float deltaZ = Input.GetAxis ( "Vertical" ) * speed; Vector3 movement = new Vector3 (deltaX, 0, deltaZ); movement = Vector3.ClampMagnitude (movement, speed); //保持各方向的速度相同 movement.y = gravity; movement = movement * Time.deltaTime; //向量可以直接乘以一个数,Time.deltaTime确保在每台计算机上的速度相同 movement = transform.TransformDirection (movement); //将本地坐标转换为世界坐标 _characterController.Move(movement); //通过CharacterController进行移动而不是transform.Translate } } |
问题
人物移动到父容器的边缘时,人物的移动会产生偏移(甚至飞起来)。将父容器扩大,使人物无法接近边缘则不会有异常。
父容器比较大
父容器比较小,人物到达边缘
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日