update: now player uses additional collider to detect ground
This commit is contained in:
@@ -1,5 +1,71 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1 &330305229598668716
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 7488478172684744634}
|
||||
- component: {fileID: 1379039836545439140}
|
||||
- component: {fileID: 1895871329809910261}
|
||||
m_Layer: 0
|
||||
m_Name: GroundDetector
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!4 &7488478172684744634
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 330305229598668716}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: -1, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 6890030909251295590}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!65 &1379039836545439140
|
||||
BoxCollider:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 330305229598668716}
|
||||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_LayerOverridePriority: 0
|
||||
m_IsTrigger: 1
|
||||
m_ProvidesContacts: 0
|
||||
m_Enabled: 1
|
||||
serializedVersion: 3
|
||||
m_Size: {x: 0.9, y: 0.2, z: 0.9}
|
||||
m_Center: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &1895871329809910261
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 330305229598668716}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: f16350ecb17bc8e49a8e9b8124ff2db2, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!1 &1084592339258698369
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -206,6 +272,7 @@ Transform:
|
||||
m_Children:
|
||||
- {fileID: 4506534449155536579}
|
||||
- {fileID: 2033391409425088012}
|
||||
- {fileID: 7488478172684744634}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!33 &1892283461227420150
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class GroundDetector : MonoBehaviour
|
||||
{
|
||||
public bool onGround { get; private set; } = false;
|
||||
|
||||
void OnTriggerEnter(Collider other) {
|
||||
if (other.CompareTag("Ground")) onGround = true;
|
||||
}
|
||||
void OnTriggerStay(Collider other) { OnTriggerEnter(other); }
|
||||
void OnTriggerExit(Collider other) {
|
||||
if (other.CompareTag("Ground")) onGround = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f16350ecb17bc8e49a8e9b8124ff2db2
|
||||
@@ -17,16 +17,18 @@ public class PlayerMovement : MonoBehaviour
|
||||
[Header("Debug info")]
|
||||
[SerializeField] bool isCrouching = false;
|
||||
[SerializeField] Vector3 gravityVelocity;
|
||||
[SerializeField] bool grounded;
|
||||
[SerializeField] bool grounded = false;
|
||||
|
||||
CharacterController characterController;
|
||||
GroundDetector ground;
|
||||
|
||||
void Start() {
|
||||
characterController = GetComponent<CharacterController>();
|
||||
ground = GetComponent<GroundDetector>();
|
||||
}
|
||||
|
||||
void Update() {
|
||||
grounded = GetOnGround();
|
||||
grounded = ground.onGround;
|
||||
HandleCrouching();
|
||||
HandleMovement();
|
||||
HandleJumping();
|
||||
@@ -69,10 +71,6 @@ public class PlayerMovement : MonoBehaviour
|
||||
characterController.Move(gravityVelocity * (Time.deltaTime * gravityMultiplier));
|
||||
}
|
||||
|
||||
bool GetOnGround() {
|
||||
return Physics.CheckSphere(legsPosition.position, legsRadius, groundMask);
|
||||
}
|
||||
|
||||
void OnControllerColliderHit(ControllerColliderHit hit) {
|
||||
hit.gameObject.SendMessage("ReverseOnControllerColliderHit", hit, SendMessageOptions.DontRequireReceiver);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user