tweaked rigidbody values and jump mechanic, still incomplete tho

This commit is contained in:
louishorlaville 2022-10-19 23:49:26 -04:00
parent 577acaa515
commit 740f235160
3 changed files with 72 additions and 19 deletions

View File

@ -416,6 +416,7 @@ MonoBehaviour:
canJump: 1
movementSpeed: 1.5
jumpPower: 5
groundDrag: 0.2
isGrounded: 0
isJumping: 0
--- !u!1 &684542796
@ -544,6 +545,7 @@ GameObject:
- component: {fileID: 1765725231}
- component: {fileID: 1765725230}
- component: {fileID: 1765725229}
- component: {fileID: 1765725232}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
@ -617,3 +619,36 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1765725232
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1765725228}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a79441f348de89743a2939f4d699eac1, type: 3}
m_Name:
m_EditorClassIdentifier:
m_RenderShadows: 1
m_RequiresDepthTextureOption: 2
m_RequiresOpaqueTextureOption: 2
m_CameraType: 0
m_Cameras: []
m_RendererIndex: -1
m_VolumeLayerMask:
serializedVersion: 2
m_Bits: 1
m_VolumeTrigger: {fileID: 0}
m_VolumeFrameworkUpdateModeOption: 2
m_RenderPostProcessing: 0
m_Antialiasing: 0
m_AntialiasingQuality: 2
m_StopNaN: 0
m_Dithering: 0
m_ClearDepth: 1
m_AllowXRRendering: 1
m_RequiresDepthTexture: 0
m_RequiresColorTexture: 0
m_Version: 2

View File

@ -1973,7 +1973,7 @@ Rigidbody:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1211811536}
serializedVersion: 2
m_Mass: 5
m_Mass: 2
m_Drag: 0.01
m_AngularDrag: 0.05
m_UseGravity: 1
@ -2082,9 +2082,12 @@ MonoBehaviour:
m_EditorClassIdentifier:
rb: {fileID: 1211811541}
canWalk: 1
canJump: 1
movementSpeed: 1.5
canJump: 0
movementSpeed: 0.8
jumpPower: 5
groundDrag: 1
airDrag: 0
playerHeight: 0.5
isGrounded: 0
isJumping: 0
--- !u!1 &1301531512
@ -2241,7 +2244,7 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1530935835}
m_LocalRotation: {x: 0.000000001639493, y: 0.9994022, z: -0.03457181, w: 0.000000047394476}
m_LocalRotation: {x: -7.4356965e-11, y: 0.9994022, z: -0.03457181, w: -0.0000000021495121}
m_LocalPosition: {x: 4.54, y: 6.64, z: 22.81}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@ -3172,7 +3175,7 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2129712287}
m_LocalRotation: {x: 0.000000001639493, y: 0.9994022, z: -0.03457181, w: 0.000000047394476}
m_LocalRotation: {x: -7.4356965e-11, y: 0.9994022, z: -0.03457181, w: -0.0000000021495121}
m_LocalPosition: {x: 4.54, y: 6.64, z: 22.81}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0

View File

@ -16,8 +16,12 @@ public class CharacterMovement : MonoBehaviour
[SerializeField] private float jumpPower;
[Header("Character properties")]
[SerializeField] private float groundDrag;
[SerializeField] private float airDrag;
[SerializeField] private float playerHeight;
public bool isGrounded;
public bool isJumping;
private Vector3 rawInputMovement;
@ -25,21 +29,20 @@ public class CharacterMovement : MonoBehaviour
{
if (canWalk || canJump)
{
if (isGrounded)
{
//The walk is actually handled here
rb.AddForce(rawInputMovement * movementSpeed, ForceMode.Impulse);
}
}
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * 1, Color.yellow);
//The walk is actually handled here
rb.AddForce(new Vector3(rawInputMovement.x * movementSpeed, rawInputMovement.y, rawInputMovement.z * movementSpeed), ForceMode.Impulse);
}
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * playerHeight, Color.yellow);
//Ground Check
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 1f, LayerMask.GetMask("Floor")))
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, playerHeight, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), out hit, playerHeight, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, playerHeight, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out hit, playerHeight, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, playerHeight, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, playerHeight, LayerMask.GetMask("Floor")))
{
isGrounded = true;
}
@ -48,6 +51,15 @@ public class CharacterMovement : MonoBehaviour
rawInputMovement = new Vector3(rawInputMovement.x, 0, rawInputMovement.z);
isGrounded = false;
}
//Handle Grounded
if (isGrounded)
{
rb.drag = groundDrag;
isJumping = false;
}
else
rb.drag= airDrag;
}
public void Walk(InputAction.CallbackContext value){
@ -63,7 +75,10 @@ public class CharacterMovement : MonoBehaviour
{
if(canJump && isGrounded)
{
rb.velocity = new Vector3(0, jumpPower, 0);
isJumping = true;
float inputMovement = value.ReadValue<float>();
rb.velocity = new Vector3(rb.velocity.x, jumpPower* inputMovement, rb.velocity.z);
//rawInputMovement = new Vector3(rawInputMovement.x, jumpPower, rawInputMovement.z);
}
}