Приседать не работает, но скользить и бежать можноC#

Место общения программистов C#
Ответить
Anonymous
 Приседать не работает, но скользить и бежать можно

Сообщение Anonymous »

Код: Выделить всё

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
private float moveSpeed;
public float walkSpeed;
public float sprintSpeed;

public bool sliding;
public bool sprinting;

public float groundDrag;

[Header("Jumping")]
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;

[Header("Crouching")]
public float crouchSpeed;
public float crouchYScale;
private float startYScale;

[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;
public KeyCode sprintKey = KeyCode.LeftShift;
public KeyCode crouchKey = KeyCode.LeftControl;

[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;

[Header("Slope Handling")]
public float maxSlopeAngle;
private RaycastHit slopeHit;
private bool exitingSlope;

[Header("Sliding")]
public float maxSlideTime;
public float slideForce;
private float slideTimer;
public float slideSpeed;

public float slideYScale;

[Header("References")]
public Transform orientation;
private Rigidbody rb;

[Header("Input")]
private float horizontalInput;
private float verticalInput;

Vector3 moveDirection;

public MovementState state;
public enum MovementState
{
walking,
sprinting,
crouching,
sliding,
air
}

private void Start()
{
rb = GetComponent();
rb.freezeRotation = true;

readyToJump = true;

startYScale = transform.localScale.y;

}

private void Update()
{
// ground check
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);

MyInput();
SpeedControl();
StateHandler();

if (Input.GetKeyUp(sprintKey) || Input.GetKeyUp(KeyCode.W))
sprinting = false;

// handle drag
if (grounded)
rb.drag = groundDrag;
else
rb.drag = 0;

horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");

if (Input.GetKeyDown(crouchKey) && sprinting)
StartSlide();

if (Input.GetKeyUp(crouchKey) && sliding)
StopSlide();
}

private void FixedUpdate()
{
MovePlayer();

if (sliding)
SlidingMovement();
}

private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");

// when to jump
if (Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;

Jump();

Invoke(nameof(ResetJump), jumpCooldown);
}

// start crouch
if (Input.GetKeyDown(crouchKey))
{
transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
}

if (Input.GetKeyDown(crouchKey) &&  sprinting)
{
transform.localScale = new Vector3(transform.localScale.x, slideYScale, transform.localScale.z);
rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
sliding = true;
moveSpeed = slideSpeed;
}

// stop crouch
if (Input.GetKeyUp(crouchKey))
{
transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
sliding = false;
}
}

private void StateHandler()
{
// Mode - Crouching
if (Input.GetKey(crouchKey))
{
state = MovementState.crouching;
moveSpeed = crouchSpeed;
}

// Mode - Sliding
if(Input.GetKey(crouchKey) && sprinting)
{
state = MovementState.sliding;
moveSpeed = slideSpeed;
}

// Mode - Sprinting
else if (grounded && Input.GetKey(sprintKey) && Input.GetKey(KeyCode.W))
{
state = MovementState.sprinting;
moveSpeed = sprintSpeed;

sprinting = true;
}

// Mode - Walking
else if (grounded)
{
state = MovementState.walking;
moveSpeed = walkSpeed;
}

// Mode - Air
else
{
state = MovementState.air;
}
}

private void MovePlayer()
{
// calculate movement direction
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;

// on slope
if (OnSlope() && !exitingSlope)
{
rb.AddForce(GetSlopeMoveDirection() * moveSpeed * 20f, ForceMode.Force);

if (rb.velocity.y > 0)
rb.AddForce(Vector3.down * 80f, ForceMode.Force);
}

// on ground
else if (grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);

// in air
else if (!grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);

// turn gravity off while on slope
rb.useGravity = !OnSlope();
}

private void SpeedControl()
{
// limiting speed on slope
if (OnSlope() && !exitingSlope)
{
if (rb.velocity.magnitude > moveSpeed)
rb.velocity = rb.velocity.normalized * moveSpeed;
}

// limiting speed on ground or in air
else
{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

// limit velocity if needed
if (flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
}
}

private void Jump()
{
exitingSlope = true;

// reset y velocity
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
readyToJump = true;

exitingSlope = false;
}

private bool OnSlope()
{
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight * 0.5f + 0.3f))
{
float angle = Vector3.Angle(Vector3.up, slopeHit.normal);
return angle < maxSlopeAngle &&  angle != 0;
}

return false;
}

private Vector3 GetSlopeMoveDirection()
{
return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized;
}

private void StartSlide()
{
sliding = true;

transform.localScale = new Vector3(transform.localScale.x, slideYScale, transform.localScale.z);
rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);

slideTimer = maxSlideTime;
}

private void SlidingMovement()
{
Vector3 inputDirection = orientation.forward;

rb.AddForce(inputDirection.normalized * slideForce, ForceMode.Force);

slideTimer -= Time.deltaTime;

if (slideTimer 

Подробнее здесь: [url]https://stackoverflow.com/questions/72897333/crouching-is-not-working-but-sliding-and-sprinting-is[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»