Ciro Continisio
4 年前
当前提交
ee2cec18
共有 14 个文件被更改,包括 476 次插入 和 414 次删除
-
134UOP1_Project/Assets/Prefabs/Pig.prefab
-
367UOP1_Project/Assets/Scenes/CharController.unity
-
6UOP1_Project/Assets/Scripts/InputReader.cs
-
4UOP1_Project/Assets/Settings/Graphics/UniversalRP-HighQuality.asset
-
2UOP1_Project/ProjectSettings/QualitySettings.asset
-
30UOP1_Project/Assets/Scripts/CameraManager.cs
-
11UOP1_Project/Assets/Scripts/CameraManager.cs.meta
-
8UOP1_Project/Assets/Scripts/Characters.meta
-
118UOP1_Project/Assets/Scripts/Characters/Character.cs
-
11UOP1_Project/Assets/Scripts/Characters/Character.cs.meta
-
64UOP1_Project/Assets/Scripts/Characters/Protagonist.cs
-
135UOP1_Project/Assets/Scripts/Protagonist.cs
-
0/UOP1_Project/Assets/Scripts/Characters/Protagonist.cs.meta
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using Cinemachine; |
|||
using System; |
|||
|
|||
public class CameraManager : MonoBehaviour |
|||
{ |
|||
public InputReader inputReader; |
|||
public CinemachineFreeLook freeLookVCam; |
|||
|
|||
private void OnEnable() |
|||
{ |
|||
inputReader.cameraMoveEvent += OnCameraMove; |
|||
//...
|
|||
} |
|||
|
|||
//Removes all listeners to the events coming from the InputReader script
|
|||
private void OnDisable() |
|||
{ |
|||
inputReader.cameraMoveEvent -= OnCameraMove; |
|||
//...
|
|||
} |
|||
|
|||
private void OnCameraMove(Vector2 cameraMovement) |
|||
{ |
|||
freeLookVCam.m_XAxis.m_InputAxisValue = cameraMovement.x * Time.smoothDeltaTime; |
|||
freeLookVCam.m_YAxis.m_InputAxisValue = cameraMovement.y * Time.smoothDeltaTime; |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 085156cba0e34b540aeddafe12d1e2f1 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 3f8fcc882e723a54e84a498e2b33474e |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
public class Character : MonoBehaviour |
|||
{ |
|||
private CharacterController characterController; |
|||
|
|||
[Tooltip("Horizontal XZ plane speed multiplier")] public float speed = 8f; |
|||
[Tooltip("General multiplier for gravity (affects jump and freefall)")] public float gravityMultiplier = 5f; |
|||
[Tooltip("The initial upwards push when pressing jump. This is injected into verticalMovement, and gradually cancelled by gravity")] public float initialJumpForce = 10f; |
|||
[Tooltip("How long can the player hold the jump button")] public float jumpInputDuration = .4f; |
|||
[Tooltip("Represents how fast gravityContributionMultiplier will go back to 1f. The higher, the faster")] public float gravityComebackMultiplier = 15f; |
|||
[Tooltip("The maximum speed reached when falling (in units/frame)")] public float maxFallSpeed = 50f; |
|||
[Tooltip("Each frame while jumping, gravity will be multiplied by this amount in an attempt to 'cancel it' (= jump higher)")] public float gravityDivider = .6f; |
|||
|
|||
private float gravityContributionMultiplier = 0f; //The factor which determines how much gravity is affecting verticalMovement
|
|||
private bool isJumping = false; //If true, a jump is in effect and the player is holding the jump button
|
|||
private float jumpBeginTime = -Mathf.Infinity; //Time of the last jump
|
|||
private float verticalMovement = 0f; //Represents how much a player will move vertically in a frame. Affected by gravity * gravityContributionMultiplier
|
|||
private Vector3 inputVector; //Initial input horizontal movement (y == 0f)
|
|||
private Vector3 movementVector; //Final movement vector
|
|||
|
|||
private void Awake() |
|||
{ |
|||
characterController = GetComponent<CharacterController>(); |
|||
} |
|||
|
|||
private void Update() |
|||
{ |
|||
//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; |
|||
} |
|||
|
|||
//Reduce the influence of the gravity while holding the Jump button
|
|||
if(isJumping) |
|||
{ |
|||
//The player can only hold the Jump button for so long
|
|||
if(Time.time >= jumpBeginTime + jumpInputDuration) |
|||
{ |
|||
isJumping = false; |
|||
gravityContributionMultiplier = 1f; //Gravity influence is reset to full effect
|
|||
} |
|||
else |
|||
{ |
|||
gravityContributionMultiplier *= gravityDivider; //Reduce the gravity effect
|
|||
} |
|||
} |
|||
|
|||
//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
|
|||
|
|||
//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; |
|||
} |
|||
} |
|||
|
|||
//Apply the result and move the character in space
|
|||
movementVector.y = verticalMovement; |
|||
characterController.Move(movementVector * Time.deltaTime); |
|||
|
|||
//Rotate to the movement direction
|
|||
movementVector.y = 0f; |
|||
if(movementVector.sqrMagnitude >= .02f) |
|||
{ |
|||
transform.forward = movementVector.normalized; |
|||
} |
|||
} |
|||
|
|||
//---- COMMANDS ISSUED BY OTHER SCRIPTS ----
|
|||
|
|||
public void Move(Vector3 movement) |
|||
{ |
|||
inputVector = movement; |
|||
} |
|||
|
|||
public void Jump() |
|||
{ |
|||
if(characterController.isGrounded) |
|||
{ |
|||
isJumping = true; |
|||
jumpBeginTime = Time.time; |
|||
verticalMovement = initialJumpForce; //This is the only place where verticalMovement is set to a positive value
|
|||
gravityContributionMultiplier = 0f; |
|||
} |
|||
} |
|||
|
|||
public void CancelJump() |
|||
{ |
|||
isJumping = false; //This will stop the reduction to the gravity, which will then quickly pull down the character
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 4eb0e4f8f5e39624ea81504bf968dcce |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
public class Protagonist : MonoBehaviour |
|||
{ |
|||
public InputReader inputReader; |
|||
public Transform gameplayCamera; |
|||
|
|||
private Character charScript; |
|||
private bool controlsEnabled = true; |
|||
|
|||
private void Awake() |
|||
{ |
|||
charScript = GetComponent<Character>(); |
|||
} |
|||
|
|||
//Adds listeners for events being triggered in the InputReader script
|
|||
private void OnEnable() |
|||
{ |
|||
inputReader.jumpEvent += OnJumpInitiated; |
|||
inputReader.jumpCanceledEvent += OnJumpCanceled; |
|||
inputReader.moveEvent += OnMove; |
|||
//...
|
|||
} |
|||
|
|||
//Removes all listeners to the events coming from the InputReader script
|
|||
private void OnDisable() |
|||
{ |
|||
inputReader.jumpEvent -= OnJumpInitiated; |
|||
inputReader.jumpCanceledEvent -= OnJumpCanceled; |
|||
inputReader.moveEvent -= OnMove; |
|||
//...
|
|||
} |
|||
|
|||
//---- EVENT LISTENERS ----
|
|||
|
|||
private void OnMove(Vector2 movement) |
|||
{ |
|||
if(controlsEnabled) |
|||
{ |
|||
//Get the two axes from the camera and flatten them on the XZ plane
|
|||
Vector3 cameraForward = gameplayCamera.forward; |
|||
cameraForward.y = 0f; |
|||
Vector3 cameraRight = gameplayCamera.right; |
|||
cameraRight.y = 0f; |
|||
|
|||
//Use the two axes, modulated by the corresponding inputs, and construct the final vector
|
|||
Vector3 adjustedMovement = cameraRight.normalized * movement.x + cameraForward.normalized * movement.y; |
|||
|
|||
charScript.Move(Vector3.ClampMagnitude(adjustedMovement, 1f)); |
|||
} |
|||
} |
|||
|
|||
private void OnJumpInitiated() |
|||
{ |
|||
if(controlsEnabled) charScript.Jump(); |
|||
} |
|||
|
|||
private void OnJumpCanceled() |
|||
{ |
|||
if(controlsEnabled) charScript.CancelJump(); |
|||
} |
|||
} |
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
public class Protagonist : MonoBehaviour |
|||
{ |
|||
public InputReader inputReader; |
|||
private CharacterController characterController; |
|||
|
|||
public float speed = 10f; //Horizontal plane speed multiplier
|
|||
public float gravityMultiplier = 5f; //General multiplier for gravity (affects jump and freefall)
|
|||
public float initialJumpForce = 10f; //The initial upwards push when pressing jump. This is injected into verticalMovement, and gradually cancelled by gravity
|
|||
public float jumpInputDuration = .4f; //How long can the player hold the jump button
|
|||
public float gravityComebackMultiplier = 15f; //Represents how fast gravityContributionMultiplier will go back to 1f. The higher, the faster
|
|||
public float maxFallSpeed = 50f; //The maximum speed reached when falling (in units/frame)
|
|||
public float gravityDivider = .6f; //Each frame while jumping, gravity will be multiplied by this amount in an attempt to "cancel it" (= jump higher)
|
|||
|
|||
private float gravityContributionMultiplier = 0f; //The factor which determines how much gravity is affecting verticalMovement
|
|||
private bool isJumping = false; //If true, a jump is in effect and the player is holding the jump button
|
|||
private float jumpBeginTime = -Mathf.Infinity; //Time of the last jump
|
|||
private float verticalMovement = 0f; //Represents how much a player will move vertically in a frame. Affected by gravity * gravityContributionMultiplier
|
|||
private Vector3 inputVector; //Initial input horizontal movement (y == 0f)
|
|||
private Vector3 movementVector; //Final movement vector
|
|||
|
|||
//Adds listeners for events being triggered in the InputReader script
|
|||
private void OnEnable() |
|||
{ |
|||
inputReader.jumpEvent += OnJumpInitiated; |
|||
inputReader.jumpCanceledEvent += OnJumpCanceled; |
|||
inputReader.moveEvent += OnMove; |
|||
//...
|
|||
} |
|||
|
|||
//Removes all listeners to the events coming from the InputReader script
|
|||
private void OnDisable() |
|||
{ |
|||
inputReader.jumpEvent -= OnJumpInitiated; |
|||
inputReader.jumpCanceledEvent -= OnJumpCanceled; |
|||
inputReader.moveEvent -= OnMove; |
|||
//...
|
|||
} |
|||
|
|||
private void Awake() |
|||
{ |
|||
characterController = GetComponent<CharacterController>(); |
|||
} |
|||
|
|||
private void Update() |
|||
{ |
|||
//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; |
|||
} |
|||
|
|||
//Reduce the influence of the gravity while holding the Jump button
|
|||
if(isJumping) |
|||
{ |
|||
//The player can only hold the Jump button for so long
|
|||
if(Time.time >= jumpBeginTime + jumpInputDuration) |
|||
{ |
|||
isJumping = false; |
|||
gravityContributionMultiplier = 1f; //Gravity influence is reset to full effect
|
|||
} |
|||
else |
|||
{ |
|||
gravityContributionMultiplier *= gravityDivider; //Reduce the gravity effect
|
|||
} |
|||
} |
|||
|
|||
//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
|
|||
|
|||
//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; |
|||
} |
|||
} |
|||
|
|||
//Apply the result and move the character in space
|
|||
movementVector.y = verticalMovement; |
|||
characterController.Move(movementVector * Time.deltaTime); |
|||
|
|||
//Rotate to the movement direction
|
|||
movementVector.y = 0f; |
|||
if(movementVector.sqrMagnitude >= .02f) |
|||
{ |
|||
transform.forward = movementVector.normalized; |
|||
} |
|||
} |
|||
|
|||
private void OnMove(Vector2 movement) |
|||
{ |
|||
inputVector = new Vector3(movement.x, 0f, movement.y); |
|||
} |
|||
|
|||
private void OnJumpInitiated() |
|||
{ |
|||
if(characterController.isGrounded) |
|||
{ |
|||
isJumping = true; |
|||
jumpBeginTime = Time.time; |
|||
verticalMovement = initialJumpForce; //This is the only place where verticalMovement is set to a positive value
|
|||
gravityContributionMultiplier = 0f; |
|||
} |
|||
} |
|||
|
|||
private void OnJumpCanceled() |
|||
{ |
|||
isJumping = false; //This will stop the reduction to the gravity, which will then quickly pull down the character
|
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue