Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [System.Serializable]
- public class CharacterMotor : MonoBehaviour
- {
- public struct State
- {
- public Vector3 position;
- public Vector3 localPosition;
- public Vector3 velocity;
- public bool isGrounded;
- }
- CharacterController characterController;
- [Header("Movement")]
- [SerializeField]
- private float runSpeed;
- State _state;
- [SerializeField]
- LayerMask layerMask;
- Vector3 sphere
- {
- get
- {
- Vector3 p;
- p = transform.position;
- p.y += characterController.radius;
- p.y -= (0.08f * 2);
- return p;
- }
- }
- private void Awake()
- {
- characterController = GetComponent<CharacterController>();
- _state = new State();
- _state.position = transform.localPosition;
- }
- public void SetState(Vector3 position, Vector3 localPosition, Vector3 velocity, bool isGrounded)
- {
- _state.localPosition = localPosition;
- _state.position = position;
- _state.velocity = velocity;
- _state.isGrounded = isGrounded;
- transform.position = _state.position;
- }
- private void Move(Vector3 velocity)
- {
- bool isGrounded = false;
- isGrounded = isGrounded || characterController.Move(velocity * BoltNetwork.FrameDeltaTime) == CollisionFlags.Below;
- isGrounded = isGrounded || characterController.isGrounded;
- isGrounded = isGrounded || Physics.CheckSphere(sphere, characterController.radius, layerMask);
- _state.isGrounded = isGrounded;
- _state.position = transform.localPosition;
- }
- public State Move(float horizontal, float vertical)
- {
- Vector3 moveDir = new Vector3(horizontal, 0, vertical) * runSpeed;
- if(_state.isGrounded)
- {
- Move(moveDir);
- }
- Move(_state.velocity);
- _state.position = transform.position;
- _state.localPosition = transform.localPosition;
- return _state;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement