unity第三人称控制
来源:互联网 发布:熊猫加速器mac 编辑:程序博客网 时间:2024/04/28 02:55
角色移动
public class player : MonoBehaviour {
Transform m_transform,m_camera;//人物自己以及相机的对象
CharacterController controller;//Charactor Controller组件
public float MoveSpeed = 20.0f;//移动的速度
// Use this for initialization
public Animator anim;
void Start () {
m_transform = this.transform;//尽量不要再update里获取this.transform,而是这样保存起来,这样能节约性能
m_camera = GameObject.FindGameObjectWithTag ("MainCamera").transform;//
controller=this.GetComponent<CharacterController>();
anim = this.GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate () {
if ((Input.GetKey (KeyCode.W)) || (Input.GetKey (KeyCode.S)) || (Input.GetKey (KeyCode.A)) || (Input.GetKey (KeyCode.D))) {
if (Input.GetKey (KeyCode.W)) {
//根据主相机的朝向决定人物的移动方向,下同
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y, 0);
}
if (Input.GetKey (KeyCode.S)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y + 180f, 0);
}
if (Input.GetKey (KeyCode.A)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y + 270f, 0);
}
if (Input.GetKey (KeyCode.D)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y + 90f, 0);
}
controller.Move (m_transform.forward * Time.deltaTime * MoveSpeed);
anim.SetBool ("torun", true);
} else
//静止状态
if (Input.GetKey (KeyCode.Q)) {
transform.Translate (Vector3.up * Time.deltaTime * MoveSpeed);
}
if (!controller.isGrounded) {
//模拟简单重力,每秒下降10米,当然你也可以写成抛物线
controller.Move(new Vector3(0,-10f*Time.deltaTime,0));
}
}
}
摄像机跟随角色
public class follow : MonoBehaviour {
public Transform pivot; // the object being followed
public Vector3 pivotOffset = Vector3.zero; // offset from target's pivot
public Transform target; // like a selected object (used with checking if objects between cam and target)
public float distance = 10.0f; // distance from target (used with zoom)
public float minDistance = 2f;
public float maxDistance = 15f;
public float zoomSpeed = 1f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public bool allowYTilt = true;
public float yMinLimit = 30f;
public float yMaxLimit = 80f;
private float x = 0.0f;
private float y = 0.0f;
private float targetX = 0f;
private float targetY = 0f;
private float targetDistance = 0f;
private float xVelocity = 1f;
private float yVelocity = 1f;
private float zoomVelocity = 1f;
void Start()
{
var angles = transform.eulerAngles;
targetX = x = angles.x;
targetY = y = ClampAngle(angles.y, yMinLimit, yMaxLimit);
targetDistance = distance;
}
void LateUpdate()
{
if (pivot)
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll > 0.0f) targetDistance -= zoomSpeed;
else if (scroll < 0.0f) targetDistance += zoomSpeed;
targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// right mouse button must be held down to tilt/rotate cam
// or player can use the left mouse button while holding Ctr
if (Input.GetMouseButton(1) || (Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) ))
{
targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
if (allowYTilt)
{
targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);
}
}
x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
if (allowYTilt) y = Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f);
else y = targetY;
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// apply
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;
transform.rotation = rotation;
transform.position = position;
}
}
private float ClampAngle(float angle, float min, float max)
{
if (angle < -360) angle += 360;
if (angle > 360) angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
- unity第三人称控制
- Unity实现第三人称角色控制
- unity 第三人称
- 第三人称控制
- unity中实现的第三人称摄像机控制脚本。
- Unity第三人称视野
- Unity 第三人称镜头跟随
- unity第三人称视角分析
- 第三人称游戏人物控制
- 第三人称蓝图控制
- unity 第三人称控制对象平移转向C#脚本(亲测有效)
- 可以控制的第三人称摄像机
- Unity3d--第三人称角色移动控制
- 【小松教你手游开发】【unity实用技能】Unity3D研究院之第一人称第三人称角色控制组件修改C#版本
- Unity射线应用(第三人称Demo制作)
- [unity for android]第三人称视角研究
- Unity射线应用(第三人称Demo制作)
- Unity第一人称和第三人称视角脚本
- Java Web中过滤器(Filter)编写示例
- zynq IRQ_P2F ----ps中断pl
- SpringMVC纯注解配置web.xml、json、静态资源、thymeleaf,递归json数组
- Rapid精英板与JavaScript玩转物联网
- 2017湖南安博会 | 展示千视通深耕深度学习原创技术风采
- unity第三人称控制
- 大白话讲解信号处理中的频谱泄露
- Oracle12C--可变数组(三十三)
- RabbitMq
- 多线程爬虫
- python基础 26章 项目7 自定义电子公告板
- Linux进程环境变量简介
- Linux文件系统
- 2017-05-22