浏览代码

More character controller jump tweaks

/main
Ciro Continisio 4 年前
当前提交
1e3626e8
共有 3 个文件被更改,包括 76 次插入28 次删除
  1. 23
      Assets/Scenes/CharController.unity
  2. 79
      Assets/Scripts/Protagonist.cs
  3. 2
      ProjectSettings/QualitySettings.asset

23
Assets/Scenes/CharController.unity


m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 308908774}
m_LocalRotation: {x: -0.06795407, y: 0.21925889, z: -0.15772013, w: 0.9604333}
m_LocalPosition: {x: 5.293609, y: 0.38725814, z: 19.685265}
m_LocalPosition: {x: 6.84, y: 0.38725814, z: 1.01}
m_LocalScale: {x: 5.7737727, y: 1.8967, z: 4.9019}
m_Children: []
m_Father: {fileID: 0}

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1567022179}
m_LocalRotation: {x: -0.018408407, y: 0.22880895, z: 0.055927288, w: 0.97168916}
m_LocalPosition: {x: 0.631511, y: 1.0090771, z: 21.949131}
m_LocalRotation: {x: 0.026077801, y: 0.22806217, z: 0.24232899, w: 0.9426475}
m_LocalPosition: {x: 2.8078, y: 0.051378, z: 3.0279}
m_LocalEulerAnglesHint: {x: -3.5180001, y: 26.324001, z: 5.7650003}
m_LocalEulerAnglesHint: {x: -3.5180001, y: 26.324001, z: 28.011002}
--- !u!1 &1724036686
GameObject:
m_ObjectHideFlags: 0

type: 3}
propertyPath: jumpInputDuration
value: 0.4
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: initialJumpForce
value: 10
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: gravityDivider
value: 0.7
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: gravityComebackMultiplier
value: 5
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240709, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}

79
Assets/Scripts/Protagonist.cs


private CharacterController characterController;
public float speed = 10f;
public float gravityMultiplier = 10f;
public float jumpMultiplier = 5f;
public float gravityMultiplier = 5f;
public float jumpInputDuration = 1f;
public float jumpInputDuration = .4f;
private float gravityCancel = 1f;
[SerializeField] private float gravityContributionMultiplier = 0f;
[SerializeField] private float gravityDivider = .7f;
[SerializeField] private float gravityComebackMultiplier = 5f;
private Vector3 finalMovementVector;
private Vector3 inputVector;
//Adds listeners for events being triggered in the InputReader script
private void OnEnable()

private void Update()
{
finalMovementVector = movementVector * speed;
gravityCancel += Time.deltaTime * 10f;
//Raises the multiplier to how much gravity will affect vertical movement when in mid-air
//This is 0f at the beginning of a jump and will raise to maximum 1f
if(!characterController.isGrounded)
{
gravityContributionMultiplier += Time.deltaTime * gravityComebackMultiplier;
}
//Jump input timeout
//Reduce the influence of the gravity while holding the Jump button
gravityCancel *= .5f;
//verticalMovement = verticalMovement + (gravityCancel * Time.deltaTime);
if(Time.time > jumpBeginTime + jumpInputDuration)
//The player can only hold the Jump button for so long
if(Time.time >= jumpBeginTime + jumpInputDuration)
gravityCancel = 1f;
gravityContributionMultiplier = 1f; //Gravity influence is reset to full effect
}
else
{
gravityContributionMultiplier *= gravityDivider; //Reduce the gravity effect
gravityCancel = Mathf.Clamp01(gravityCancel);
verticalMovement = verticalMovement + (Physics.gravity.y * gravityMultiplier * Time.deltaTime * gravityCancel); //Add gravity contribution
//Calculate the final verticalMovement
if(!characterController.isGrounded)
{
//Less control in mid-air, conserving momentum from previous frame
movementVector = inputVector * speed;
//The character is either jumping or in freefall, so gravity will add up
gravityContributionMultiplier = Mathf.Clamp01(gravityContributionMultiplier);
verticalMovement += Physics.gravity.y * gravityMultiplier * Time.deltaTime * gravityContributionMultiplier; //Add gravity contribution
//Note that even if it's added, the above value is negative due to Physics.gravity.y
verticalMovement = Mathf.Clamp(verticalMovement, -maxFallSpeed, 100f);
finalMovementVector.y = verticalMovement;
//Cap the maximum so the player doesn't reach incredible speeds when freefalling from high positions
verticalMovement = Mathf.Clamp(verticalMovement, -maxFallSpeed, 100f);
}
else
{
//Full speed ground movement
movementVector = inputVector * speed;
//Resets the verticalMovement while on the ground,
//so that regardless of whether the player landed from a high fall or not,
//if they drop off a platform they will always start with the same verticalMovement.
//-5f is a good value to make it so the player also sticks to uneven terrain/bumps without floating.
if(!isJumping)
{
verticalMovement = -5f;
gravityContributionMultiplier = 0f;
}
}
characterController.Move(finalMovementVector * Time.deltaTime);
//Apply the result and move the character in space
movementVector.y = verticalMovement;
characterController.Move(movementVector * Time.deltaTime);
movementVector.y = 0f;
if(movementVector.sqrMagnitude >= .02f)
{
transform.forward = movementVector.normalized;

private void OnMove(Vector2 movement)
{
movementVector = new Vector3(movement.x, 0f, movement.y);
inputVector = new Vector3(movement.x, 0f, movement.y);
}
private void OnJumpInitiated()

isJumping = true;
jumpBeginTime = Time.time;
verticalMovement = initialJumpForce;
gravityCancel = 0f;
verticalMovement = initialJumpForce; //This is the only place where verticalMovement is set to a positive value
gravityContributionMultiplier = 0f;
isJumping = false;
isJumping = false; //This will stop the reduction to the gravity, which will then quickly pull down the character
}
}

2
ProjectSettings/QualitySettings.asset


skinWeights: 2
textureQuality: 0
anisotropicTextures: 1
antiAliasing: 0
antiAliasing: 2
softParticles: 0
softVegetation: 1
realtimeReflectionProbes: 1

正在加载...
取消
保存