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

本文详细介绍了如何使用Unity3D的C#脚本来创建一个第一人称控制器,涵盖了移动、旋转和交互等核心功能,是Unity3D初学者掌握游戏开发的重要教程。
摘要由CSDN通过智能技术生成


<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 = tr
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值