using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallRun : MonoBehaviour
{
[Header("Wallrun")]
public LayerMask whatIsWall;
public LayerMask whatIsGround;
public float wallRunForce;
public float maxWallRunTime;
private float wallRunTimer;
[Header("Detection")]
public float wallCheckDistance;
public float minJumpHeight;
private RaycastHit leftWallHit;
private RaycastHit rightWallHit;
private bool wallLeft;
private bool wallRight;
[Header("Input")]
float horizontalInput;
float verticalInput;
[Header("References")]
public Transform orientation;
private Rigidbody rb;
private PlayerMovement pm;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
pm = GetComponent
();
}
void Update()
{
CheckForWall();
StateMachine();
}
private void FixedUpdate()
{
if (pm.wallrunning)
WallRunningMovement();
}
private void CheckForWall()
{
wallRight = Physics.Raycast(transform.position, /*Vector3.left*/orientation.right, out rightWallHit, wallCheckDistance, whatIsWall);
wallLeft = Physics.Raycast(transform.position, /*Vector3.left*/-orientation.right, out leftWallHit, wallCheckDistance, whatIsWall);
}
private bool AboveGround()
{
return !Physics.Raycast(transform.position, Vector3.down, minJumpHeight, whatIsGround);
}
private void StateMachine()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
if((wallLeft || wallRight) && verticalInput > 0 && AboveGround())
{
if (!pm.wallrunning)
{
StartWallRun();
}
else
{
if(pm.wallrunning)
StopWallRun();
}
}
}
private void StartWallRun()
{
pm.wallrunning = true;
}
private void WallRunningMovement()
{
rb.useGravity = false;
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;
Vector3 wallForward = Vector3.Cross(wallNormal, transform.up);
rb.AddForce(wallForward * wallRunForce, ForceMode.Force);
}
private void StopWallRun()
{
pm.wallrunning = false;
}
}
Потратил много времени, но так и не смог решить этот вопрос. Думал, что раз у меня только одна гос проблема, сделал гос гулять но проблема была не в этом. Пробовал сделать активацию гравитации после выхода из состояния бега по стене, тоже не получилось.
Сделайте систему бега по стене, персонаж входит в состояние бега по стене, но не выходит из него. Вот видео: [youtube]y4POPMRsjg4[/youtube] [code]PlayerMovement using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine;
public class PlayerMovement : MonoBehaviour { [Header("Speed Display")] public float speed; //переменная для проверки скорости
[Header("Movement")] public float moveSpeed;
public float groundDrag;
public float jumpForce; public float jumpCooldown; public float airMultiplayer; public float wallRunSpeed; bool readyToJump = false;
[Header("Keybinds")] public KeyCode jumpKey = KeyCode.Space;
[Header("Ground Check")] public float playerHeight; public LayerMask whatIsGround; bool grounded;
[Header("References")] public Transform orientation; private Rigidbody rb;
public MovementState state; public enum MovementState {
wallrunning
}
public bool wallrunning; [Header("Input")] float horizontalInput; float verticalInput;
Vector3 moveDirection;
private void Start() { //gameObject.GetComponent ().enabled = false; StartCoroutine(CalcSpeed()); rb = GetComponent(); rb.freezeRotation = true; //если не будет фриза, то игрок упадет (?) readyToJump = true; }
private void StateHandler() { //Mode - wallrunning if (wallrunning) { state = MovementState.wallrunning; moveSpeed = wallRunSpeed; } } } [/code] Wallrun [code]using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WallRun : MonoBehaviour { [Header("Wallrun")] public LayerMask whatIsWall; public LayerMask whatIsGround; public float wallRunForce; public float maxWallRunTime; private float wallRunTimer;
[Header("Detection")] public float wallCheckDistance; public float minJumpHeight; private RaycastHit leftWallHit; private RaycastHit rightWallHit; private bool wallLeft; private bool wallRight;
private void StopWallRun() { pm.wallrunning = false; } } [/code] Потратил много времени, но так и не смог решить этот вопрос. Думал, что раз у меня только одна гос проблема, сделал гос гулять но проблема была не в этом. Пробовал сделать активацию гравитации после выхода из состояния бега по стене, тоже не получилось.