浏览代码

Using default CharacterController

/main
Ciro Continisio 4 年前
当前提交
912d96c4
共有 3 个文件被更改,包括 108 次插入40 次删除
  1. 60
      Assets/Scenes/CharController.unity
  2. 5
      Assets/Scripts/InputReader.cs
  3. 83
      Assets/Scripts/Protagonist.cs

60
Assets/Scenes/CharController.unity


m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 135922103}
m_LocalRotation: {x: 0.24395259, y: 0, z: 0, w: 0.9697872}
m_LocalPosition: {x: 0.62, y: 5.43, z: -10.46}
m_LocalRotation: {x: 0.2439526, y: 0, z: 0, w: 0.9697872}
m_LocalPosition: {x: 0.62, y: 15.43, z: -10.46}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 2020721999}

m_Icon: {fileID: 7422848464349959988, guid: 0000000000000000d000000000000000, type: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &847350331
Transform:
m_ObjectHideFlags: 0

m_Icon: {fileID: 7422848464349959988, guid: 0000000000000000d000000000000000, type: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &1297158709
Transform:
m_ObjectHideFlags: 0

m_Icon: {fileID: 7422848464349959988, guid: 0000000000000000d000000000000000, type: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &1545926752
Transform:
m_ObjectHideFlags: 0

m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1961065787}
m_LocalRotation: {x: 0.24395259, y: 0, z: 0, w: 0.9697872}
m_LocalPosition: {x: 0.62, y: 5.43, z: -10.46}
m_LocalRotation: {x: 0.2439526, y: 0, z: 0, w: 0.9697872}
m_LocalPosition: {x: 0.62, y: 15.43, z: -10.46}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}

propertyPath: castCentreMid
value:
objectReference: {fileID: 1545926752}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: gravityMultiplier
value: 5
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: maxFallSpeed
value: 50
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: jumpMultiplier
value: 5
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240708, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: jumpInputDuration
value: 0.4
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240709, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: m_Name

- target: {fileID: 3341179906418240731, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: 10
objectReference: {fileID: 0}
- target: {fileID: 3341179906418240731, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}

objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 0fa393e1e37bc9e4e829c25a9452bcd3, type: 3}
--- !u!1 &3341179907149091244 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 3341179906418240709, guid: 0fa393e1e37bc9e4e829c25a9452bcd3,
type: 3}
m_PrefabInstance: {fileID: 3341179907149091243}
m_PrefabAsset: {fileID: 0}
--- !u!143 &3341179907149091245
CharacterController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3341179907149091244}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Height: 1.75
m_Radius: 0.4
m_SlopeLimit: 30
m_StepOffset: 0.3
m_SkinWidth: 0.08
m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 0.875, z: 0}
--- !u!1001 &6560919939139749480
PrefabInstance:
m_ObjectHideFlags: 0

5
Assets/Scripts/InputReader.cs


public class InputReader : MonoBehaviour, GameInput.IGameplayActions
{
public UnityAction jumpEvent;
public UnityAction jumpCanceledEvent;
public UnityAction attackEvent;
public UnityAction interactEvent;
public UnityAction extraActionEvent;

if(jumpEvent != null
&& context.phase == InputActionPhase.Started)
jumpEvent.Invoke();
if(jumpCanceledEvent != null
&& context.phase == InputActionPhase.Canceled)
jumpCanceledEvent.Invoke();
}
public void OnMove(InputAction.CallbackContext context)

83
Assets/Scripts/Protagonist.cs


public class Protagonist : MonoBehaviour
{
public InputReader inputReader;
private Rigidbody rigidbody;
private CharacterController characterController;
public float airMomentumMultiplier = 3f;
public float jumpStrength = 1f;
private Vector3 momentum;
private bool isGrounded = true;
private bool jumpInitiated = false; //Becomes true on the frame that the jump button is pressed, consumed in FixedUpdate
public float gravityMultiplier = 10f;
public float jumpMultiplier = 5f;
public float initialJumpForce = 10f;
public float jumpInputDuration = 1f;
public float maxFallSpeed = 50f;
private float gravityCancel = 1f;
private bool isJumping = false;
private float jumpBeginTime = -Mathf.Infinity;
private float verticalMovement = 0f;
private Vector3 finalMovementVector;
private Vector3 movementVector;
inputReader.jumpEvent += OnJump;
inputReader.jumpEvent += OnJumpInitiated;
inputReader.jumpCanceledEvent += OnJumpCanceled;
inputReader.moveEvent += OnMove;
//...
}

private void Awake()
{
rigidbody = GetComponent<Rigidbody>();
characterController = GetComponent<CharacterController>();
private void FixedUpdate()
private void Update()
if (isGrounded)
finalMovementVector = movementVector * speed;
gravityCancel += Time.deltaTime * 10f;
//Jump input timeout
if(isJumping)
if(jumpInitiated)
gravityCancel *= .5f;
//verticalMovement = verticalMovement + (gravityCancel * Time.deltaTime);
if(Time.time > jumpBeginTime + jumpInputDuration)
//Jump!
//rigidbody.AddForce(Vector3.up * jumpStrength, ForceMode.Impulse);
rigidbody.velocity = new Vector3(momentum.x * speed, jumpStrength, momentum.z * speed);
jumpInitiated = false;
isGrounded = false;
}
else
{
//Normal movement
rigidbody.MovePosition(rigidbody.position + momentum * Time.fixedDeltaTime * speed);
isJumping = false;
gravityCancel = 1f;
else
gravityCancel = Mathf.Clamp01(gravityCancel);
verticalMovement = verticalMovement + (Physics.gravity.y * gravityMultiplier * Time.deltaTime * gravityCancel); //Add gravity contribution
verticalMovement = Mathf.Clamp(verticalMovement, -maxFallSpeed, 100f);
finalMovementVector.y = verticalMovement;
characterController.Move(finalMovementVector * Time.deltaTime);
//Rotate to the movement direction
if(movementVector.sqrMagnitude >= .02f)
//In midair
rigidbody.AddForce(momentum * airMomentumMultiplier * Time.fixedDeltaTime * speed);
transform.forward = movementVector.normalized;
Debug.Log("Move " + movement);
momentum = new Vector3(movement.x, 0f, movement.y);
movementVector = new Vector3(movement.x, 0f, movement.y);
private void OnJump()
private void OnJumpInitiated()
if (isGrounded)
if(characterController.isGrounded)
jumpInitiated = true;
isJumping = true;
jumpBeginTime = Time.time;
verticalMovement = initialJumpForce;
gravityCancel = 0f;
private void OnCollisionEnter(Collision coll)
private void OnJumpCanceled()
isGrounded = true;
rigidbody.velocity = Vector3.zero;
isJumping = false;
}
}
正在加载...
取消
保存