public class PlayerControl : MonoBehaviour
{
public float speedMove; // Скорость передвижения
private float gravityForce; // гравитации персонажа
public float gravityForceSpeed; // скорость гравитации.
private Vector3 moveVector; // Вектор движения
public float jumpForce;
private CharacterController ch_controller; //Ссылка на компонент
private void Start()
{
ch_controller = GetComponent<CharacterController>();
}
private void Update()
{
CharacterMove();
GamingGravity();
}
//Метод перемещения персонажа по сцене.
private void CharacterMove()
{
moveVector = Vector3.zero;
moveVector.x = Input.GetAxis("Horizontal") * speedMove;
moveVector.z = Input.GetAxis("Vertical") * speedMove;
// moveVector.y -= gravityForceSpeed;
moveVector.y =gravityForce;
ch_controller.Move(moveVector * Time.deltaTime);
}
//Метод гравитации.
private void GamingGravity()
{
if(!ch_controller.isGrounded) gravityForce-=gravityForceSpeed * Time.deltaTime;
else gravityForce = -1f;
if (Input.GetKey(KeyCode.Space)) gravityForce = jumpForce;
}
public int max_x;
void Update(){
if(this.transform.position.x > max_x)
this.transform.position = new Vector(max_x, this.transform.position.y,this.transform.position.z);
}