This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class ForceFriction : MonoBehaviour | |
| { | |
| [SerializeField] private BoxCollider _boxCollider; | |
| [SerializeField] private Rigidbody _rigidbody; | |
| [SerializeField] private Vector3 _overlapSize = new Vector3(0.2f, 0.2f, 0); | |
| [SerializeField] private LayerMask _GroundOnly; | |
| [SerializeField] private float _weight; | |
| [SerializeField] private float _normalForce; | |
| [SerializeField] private float _staticFriction; | |
| [SerializeField] private float _kinematicFriction; | |
| [SerializeField] private float _acceleration; | |
| [SerializeField] private float _force; | |
| private void Update() | |
| { | |
| if (CheckIsOnGrounded()) | |
| { | |
| _weight = _rigidbody.mass * Physics.gravity.y; | |
| _normalForce = _weight; | |
| _kinematicFriction = _boxCollider.material.dynamicFriction * _normalForce; | |
| _staticFriction = _boxCollider.material.staticFriction * _normalForce; | |
| _acceleration = _force / _rigidbody.mass; | |
| } | |
| } | |
| private void FixedUpdate() | |
| { | |
| if (Input.GetKey(KeyCode.Space)) | |
| { | |
| Move(); | |
| print("Force" + _rigidbody.GetAccumulatedForce()); | |
| } | |
| } | |
| private void Move() | |
| { | |
| _rigidbody.AddForce(Vector3.right * _force); | |
| } | |
| private bool CheckIsOnGrounded() | |
| { | |
| Collider[] _collider = Physics.OverlapBox(new Vector3(transform.position.x, transform.position.y + _boxCollider.center.y - 1, transform.position.z), _overlapSize, Quaternion.identity, _GroundOnly); | |
| for (int i = 0; i < _collider.Length; i++) | |
| { | |
| if (_collider != null) return true; | |
| } | |
| return false; | |
| } | |
| private void OnDrawGizmos() | |
| { | |
| Gizmos.color = Color.red; | |
| Gizmos.DrawCube(new Vector3(transform.position.x, transform.position.y + _boxCollider.center.y - 1, transform.position.z), _overlapSize / 2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment