<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
Unity3D 第一人称控制器 C#脚本
最新推荐文章于 2024-03-14 17:13:33 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)