Unity3D 第一人称控制器 C#脚本

Unity3D实现第一人称移动,随鼠标转动视角+上楼梯(C#) 阅读详情


<P> using UnityEngine;   
using System.Collections;   </P>
<P>/**   
*  @Author : <A href="http://www.xuanyusong.com">www.xuanyusong.com</A>   
*/</P>
<P>[RequireComponent(typeof(CharacterController))]   
[AddComponentMenu("Character/Character Motor")]   </P>
<P>public class CharacterMotor : MonoBehaviour {   
   
    // Does this script currently respond to input?   
    public bool canControl  = true;   
   
    public bool useFixedUpdate = true;   
   
    // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.   
    // Very handy for organization!   
   
    // The current global direction we want the character to move in.   
    [System.NonSerialized]   
    public Vector3 inputMoveDirection = Vector3.zero;   
   
    // Is the jump button held down? We use this interface instead of checking   
    // for the jump button directly so this script can also be used by AIs.   
    [System.NonSerialized]   
    public bool inputJump  = false;   
   
    [System.Serializable]   
    public class CharacterMotorMovement   
    {   
        
        // The maximum horizontal speed when moving   
        public float maxForwardSpeed = 10.0f;   
        public float maxSidewaysSpeed = 10.0f;   
        public float maxBackwardsSpeed = 10.0f;   
        
        // Curve for multiplying speed based on slope (negative = downwards)   
        public AnimationCurve slopeSpeedMultiplier = new AnimationCurve(new Keyframe(-90, 1), new Keyframe(0, 1), new Keyframe(90, 0));   
        
        // How fast does the character change speeds?  Higher is faster.   
        public float maxGroundAcceleration = 30.0f;   
        public float maxAirAcceleration = 20.0f;   
        
        // The gravity for the character   
        public float gravity = 10.0f;   
        public float maxFallSpeed = 20.0f;   
        
        // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.   
        // Very handy for organization!   
        
        // The last collision flags returned from controller.Move   
        [System.NonSerialized]   
        public CollisionFlags collisionFlags;   
        
        // We will keep track of the character's current velocity,   
        [System.NonSerialized]   
        public Vector3 velocity;   
        
        // This keeps track of our current velocity while we're not grounded   
        [System.NonSerialized]   
        public Vector3 frameVelocity = Vector3.zero;   
        
        [System.NonSerialized]   
        public Vector3 hitPoint = Vector3.zero;   
        
        [System.NonSerialized]   
        public Vector3 lastHitPoint = new Vector3(Mathf.Infinity, 0, 0);   
    }   
   
    public CharacterMotorMovement movement = new CharacterMotorMovement();   
   
    public enum MovementTransferOnJump {   
        None, // The jump is not affected by velocity of floor at all.   
        InitTransfer, // Jump gets its initial velocity from the floor, then gradualy comes to a stop.   
        PermaTransfer, // Jump gets its initial velocity from the floor, and keeps that velocity until landing.   
        PermaLocked // Jump is relative to the movement of the last touched floor and will move together with that floor.   
    }   
   
    // We will contain all the jumping related variables in one helper class for clarity.      
    [System.Serializable]   
    public class CharacterMotorJumping {   
        // Can the character jump?   
        public bool enabled = true;   
        
        // How high do we jump when pressing jump and letting go immediately   
        public float baseHeight = 1.0f;   
        
        // We add extraHeight units (meters) on top when holding the button down longer while jumping   
        public float extraHeight = 4.1f;   
        
        // How much does the character jump out perpendicular to the surface on walkable surfaces?   
        // 0 means a fully vertical jump and 1 means fully perpendicular.   
        public float perpAmount  = 0.0f;   
        
        // How much does the character jump out perpendicular to the surface on too steep surfaces?   
        // 0 means a fully vertical jump and 1 means fully perpendicular.   
        public float steepPerpAmount = 0.5f;   
        
        // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.   
        // Very handy for organization!   
        
        // Are we jumping? (Initiated with jump button and not grounded yet)   
        // To see if we are just in the air (initiated by jumping OR falling) see the grounded variable.   
        [System.NonSerialized]   
        public bool jumping = false;   
        
        [System.NonSerialized]   
        public bool holdingJumpButton = false;   
        
        // the time we jumped at (Used to determine for how long to apply extra jump power after jumping.)   
        [System.NonSerialized]   
        public float lastStartTime = 0.0f;   
        
        [System.NonSerialized]   
        public float lastButtonDownTime = -100f;   
        
        [System.NonSerialized]   
        public Vector3 jumpDir = Vector3.up;   
    }   
   
    public CharacterMotorJumping  jumping = new CharacterMotorJumping();   
   
    [System.Serializable]   
    public class CharacterMotorMovingPlatform {   
        public bool enabled = true;   
        
        public MovementTransferOnJump movementTransfer = MovementTransferOnJump.PermaTransfer;   
        
        [System.NonSerialized]   
        public Transform hitPlatform;   
        
        [System.NonSerialized]   
        public Transform activePlatform;   
        
        [System.NonSerialized]   
        public Vector3 activeLocalPoint;   
        
       &nb
[Unity3D]第一人称角色控制器 [Unity3D]最简单最详细的第一人称角色控制器 自学Unity3D有一段时间了,一直想弄一个第一人称角色控制器,网上还是有很多教程和资料,但感觉有很多教程和资料理解起来比较复杂,在这里我结合网上所学的知识自己写了一个比较容易理解的Unity3D第一人称控制器脚本。 首先来看一下完成后的效果 接下来逐步讲解如何实现这个功能 首先我们打开Unity创建一个Unity3D工程 2.在Hierarchy面板空白处单击右键,点击Create Empty创建一个空对象,点击创建好的空对象,右键单击rename 阅读详情

相关推荐

Unity3D场景漫游毕设实战:从模型处理到交互实现全流程解析

三维场景漫游是计算机图形学与虚拟现实领域的核心应用之一,它通过构建沉浸式虚拟环境,让用户能够自由探索数字空间。其技术原理涉及三维建模、实时渲染、物理模拟和人机交互等多个层面,在游戏开发、数字孪生、教育培训和文化遗产保护等领域具有重要价值。本文以热门的Unity3D引擎为基础,结合Blender等工具,详细拆解一个完整场景漫游项目的实现路径。内容涵盖从SolidWorks等工程软件模型导入、格式转换与减面优化,到地形编辑、光照烘焙与后期处理等环境搭建技巧,并深入讲解了第一人称控制器、触发交互、物品拾取等核心交

weixin_29008805的博客 336

unity3D C#脚本实现鼠标拖动物体

using System.Collections; public class clickmove : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { }

美丽的旭哥(C/C++) 2万+

Unity3d即时战斗之敌人AI和角色攻击

角色控制脚本Character.cs,为角色添加角色控制器Character controller。 using UnityEngine; using System.Collections; public class Character : MonoBehaviour { float attackInterval = 1.5f; float timer = 0; public floa

测试猫 2万+

Unity3D用c#脚本修改Animator controller的weight值

本文介绍如何通过脚本修改Animator controller的layer中的weight值

Bravo 1687

Rockchip RK3568实战:手把手教你调试I2S音频驱动(附ALSA设备树配置详解)

本文提供Rockchip RK3568平台I2S音频驱动的实战调试指南。详细解析了从硬件连接到软件配置的全过程,重点涵盖ALSA驱动框架下的设备树配置、时钟计算、DMA参数设置以及常见问题(如无声、爆音)的排查与解决方法,帮助开发者快速定位并解决音频驱动难题。

weixin_28358083的博客 495

TMC5130/TMC5160步进电机驱动芯片深度解析与工程实践

步进电机驱动芯片是精密运动控制系统的物理执行核心,其性能直接决定定位精度、运行噪声与系统鲁棒性。基于电流闭环控制与电压前馈调制的混合驱动原理,现代智能驱动芯片如TMC51X0系列实现了微步插补、堵转检测和片上运动规划等关键技术突破。这类芯片通过SPI寄存器级配置实现StealthChop静音驱动与SpreadCycle高动态响应模式的无缝切换,在3D打印、CNC、机器人关节等场景中显著提升能效比与控制带宽。本文围绕TMC5130与TMC5160的硬件架构差异、寄存器操作时序、驱动模式物理机制及FreeRTO

weixin_35797963的博客 408

欧姆龙PLC的FinsTCP协议

三个流行的PLC: PLC 通讯协议 默认IP和端口 区别 西门子S7-1200 S7 192.168.1.201 :102 2次验证(正常连接后,第一次发不断开,第二次发断开) 三菱Q系列 MC 192.168.1.250 :4096 不验证(正常连接后,怎么发都不断开) 欧姆龙CJ-2M系列 FinsTCP 192.168.1.14 :9600 1次验证(正常连接后,第一次发断开) ...

周林的博客 1万+

冗余位技术在SAR ADC中的动态误差恢复机制解析

本文深入解析了冗余位技术在SAR ADC中的动态误差恢复机制,详细介绍了其基本概念、动态误差产生原因及冗余位的容错空间设计、时序松弛策略和数字校正算法。通过实际电路设计案例和数学建模,展示了冗余位技术如何显著提升ADC的精度和能效比,同时探讨了未来发展方向。

weixin_28335283的博客 973

VMware 15 虚拟机安装Ubuntu 18.04

这几年在自动化、计算机之间游来游去,最近又要做机器学习相关的课题,14年做的虚拟机实在不堪重用了,OK,我们重新开始。 VMware的安装就不再赘述了,目前最新版本是15.0.2,官方下载地址: https://my.vmware.com/en/web/vmware/info/slug/desktop_end_user_computing/vmware_workstation_pro/15_0...

略懂 2万+

【华为机试真题 Python实现】分苹果

《华为机试真题》专栏含牛客网华为专栏、华为面经试题、华为OD机试真题。如果您在准备华为的面试,期间有想了解的可以私信我,我会尽可能帮您解答,也可以给您一些建议!本文解法非最优解(即非性能最优)。A,B两个人把苹果分为两堆,A希望按照它的计算规则等分苹果,他的计算规则是按照二进制加法计算,并且不计算走位,12+5=9(1100+0101=9), B的计算规则是十进制加法,包括正常进位,B希望在满足A的情况下获取苹果重量最多,输入苹果的数量和每个苹果重量,输出满足A的情况下获取的苹果总重量, 如果无法满足A的要

3412

最新2026年《网络数据安全风险评估办法》条款解读

大型企业集团IT变革下的零信任安全体系建设方案(55页).pptx。以数据安全为核心的安全立体防御体系解决方案(41页).pptx。让数据安全合规地创造价值(15页 PPT).pptx。大型集团数据安全应急预案(57页).docx。数据安全治理与评估服务业务介绍.pptx。数据安全管理实践(17页).pptx。大数据安全架构设计方案.pptx。大数据安全规划总体方案.pptx。数据库安全综合治理方案.pptx。医疗数据安全建设方案.pptx。数据安全框架规划图.pptx。数据安全治理方案.pptx。

cdfunlove的博客 46

GPU 运维实战:H100/H800/B300 换卡与压测

(烧多久、看什么、什么算合格)。文末还给了一个能直接跑的验收判定脚本,三个真实场景的裁决输出都在。

老韩的Linux云计算架构师进阶之路 270

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

上一篇: 最新 unity 血条的另类制作-伤害减血加血自动回血
下一篇: Unity3D优化总结
七大黍
博客等级 码龄20年 436粉丝 260原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值