1.导入unity自带的Character Controllers包

  

2.可以看到First Person Controller组件的构成

  

  Mouse Look() : 随鼠标的移动而使所属物体发生旋转

  FPSInput Controller() : 控制物体的移动

3.同样的,我们为自己的模型添加以上四个组件

  

  其中Mouse Look() 中的Axes属性,是调整围绕的旋转轴

  所谓第一人称就是,鼠标左右晃动则模型以X为轴进行旋转

  鼠标上下晃动则模型的腰关节以Z轴进行旋转

4.找到模型的腰关节,同样添加Mouse Look(),Axes的值为Y,修改Mouse Look()

 using UnityEngine;
using System.Collections; /// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation /// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
/// -> A CharacterMotor and a CharacterController component will be automatically added. /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour { public enum RotationAxes { MouseXAndY = , MouseX = , MouseY = }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F; public float minimumX = -360F;
public float maximumX = 360F; public float minimumY = -60F;
public float maximumY = 60F; private Vector3 eulerAngles; float rotationY = 0F; void LateUpdate ()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX; rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); transform.localEulerAngles = new Vector3(-rotationY, rotationX, );
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(, Input.GetAxis("Mouse X") * sensitivityX, );
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
//----------********使Z轴旋转相应的鼠标偏移********----------//
transform.localEulerAngles = new Vector3(eulerAngles.x,eulerAngles.y,eulerAngles.z + rotationY);
}
} void Start ()
{
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
//----------********获得初始的旋转********----------//
eulerAngles = transform.localEulerAngles;
}
}

  上面代码中,值得注意的是LateUpdate(),原始的为Update(),因为模型默认会有动画在不停播放,

那么播放动画的Update()也会调用模型全身的骨骼,那么就会产生冲突,也就使腰部无法上下旋转。

所以将Update()改为LateUpdate(),后于动画播放的调用,即可。

  此时就可以将Main camera添加在模型腰部组件下

 

模型的左右旋转、观察上下的视野就完成了。

  

【Unity3D】Unity自带组件—完成第一人称人物控制的更多相关文章

  1. unity3d学习笔记(一) 第一人称视角实现和倒计时实现

    unity3d学习笔记(一) 第一人称视角实现和倒计时实现 1. 第一人称视角 (1)让mainCamera和player(视角对象)同步在一起 因为我们的player是生成的,所以不能把mainCa ...

  2. 关于Unity中FPS第一人称射击类游戏制作(专题十)

    当前Unity最新版本5.6.3f1,我使用的是5.5.1f1 场景搭建 1: 导入人物模型, 手持一把枪;2: 导入碎片模型;3: 创建一个平面;4: 创建一个障碍物;5: 导入人物模型;6: 配置 ...

  3. Unity——第一人称控制器的实现

    Unity--第一人称控制器的实现 一.功能描述 在一个场景中实现人物的前后左右移动和跳跃功能:其中前后左右移动通过W.A.S.D方向键实现,跳跃功能通过空格键实现,并且考虑到重力作用,来调节跳跃功能 ...

  4. unity中制作模拟第一人称视角下的指南针

    private int zRotation; public GameObject obj; public void Update() { //obj = GameObject.Find("C ...

  5. Unity自带网络功能——NetworkView组件、Serialize、RPC

    Unity拥有大量的第三方插件,专门提供了对网络功能的支持.可是,大部分开发人员第一次接触到的还是Unity自带的网络功能,也就是大家常常说到的Unity Networking API.这些API是借 ...

  6. unity中自制模拟第一人称视角

    public float sensitivityX = 5f; public float sensitivityY = 5f; public float sensitivetyKeyBoard = 0 ...

  7. 开发一个最简单的Cardboard虚拟现实应用(四)做一个Cardboard第一人称控制器

    [开源互助-原创文章,转载请说明出处]第三帖中已经创建了一个cardboard自带的demo应用,但它是不能移动的,玩家只能站在原地,通过头部转动来观察四周,除此之外,玩家并没有更多的手段与游戏场景进 ...

  8. unity自带寻路Navmesh入门教程(三)

    继续介绍NavMesh寻路的功能,接下来阿赵打算讲一下以下两个例子,先看看完成的效果:   第一个例子对于喜欢DOTA的朋友应该很熟悉了,就是不同小队分不同路线进攻的寻路,红绿蓝三个队伍分别根据三条路 ...

  9. 【转】unity自带寻路Navmesh入门教程(三)

    http://liweizhaolili.blog.163.com/blog/static/16230744201271225812998/ 继续介绍NavMesh寻路的功能,接下来阿赵打算讲一下以下 ...

随机推荐

  1. c#中HttpWebRequest使用Proxy实现指定IP的域名请求

    原文:http://www.cnblogs.com/greenerycn/archive/2010/04/11/httpwebreques_host_modify_By_set_proxy.html ...

  2. Ubuntu 下使用Remmina Remote Desktop client 连接windows server输入法的问题

    Ubuntu 自带的Remmina Remote  Desktop 用来连接windows,vnc,ssh等非常方便好用,   但我在连接windows 2008 r2 server时遇到一个问题: ...

  3. What is XMLHTTP? How to use security zones in Internet Explorer

    Types of Security Zones Internet Zone This zone contains Web sites that are not on your computer or ...

  4. POJ 1775

    #include <iostream> using namespace std; ,,,,,,,,,}; bool boo; void DFS(int time,int sum); int ...

  5. .NET复习笔记

    .NET 基础知识点汇总 课前知识储备. 一.C#与.NET的区别? 1..NET/dotnet:一般指.Net Framework框架,一种平台,一种技术 2.C#(sharp):一种编程语言,可以 ...

  6. hdu1021 Fibonacci Again

    http://acm.hdu.edu.cn/showproblem.php?pid=1021 //找规律.. #include<iostream> #include<stdio.h& ...

  7. 安装ubuntu vi编辑无法正常使用的时候 如方向键变成ABCD

    http://blog.sina.com.cn/s/blog_7e3f6e8f0100vkon.html 在使用ubuntu的时候,发现vi编辑模式下退格键backspace和上下左右光标移动键不能用 ...

  8. 欧拉工程第60题:Prime pair sets

    题目链接 五个数,任意两个数的任意链接后的数还是质数 满足这个条件的最小五个数的和是多少? 结果:26033 纯暴力破解: package projecteuler51to60; import jav ...

  9. iOS 中有用的开源库

    youtube下载神器:https://github.com/rg3/youtube-dl vim插件:https://github.com/Valloric/YouCompleteMe vim插件配 ...

  10. Using the Repository Pattern with ASP.NET MVC and Entity Framework

    原文:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and ...