Anonymous
Метод прыжков, не применяя силы, претендует на их рассчитывание, и метод, который называется должным образом
Сообщение
Anonymous » 25 апр 2025, 13:13
Недавно перешел мой проект Unity в новую систему ввода, и моя логика прыжков не работает, несмотря на то, что метод прыжков правильно называется для Debug.logs. любая помощь ?? < /p>
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class RigidbodyMovement : MonoBehaviour
{
//remember to set player to "Agent" layer (i used layer 8) so that it doesnt just snap to itself
[SerializeField]
InputManager inputs;
[SerializeField]
Transform playerInputSpace = default;
[SerializeField, Range(0f, 100f)]
float maxSpeed = 10f;
[SerializeField, Range(0f, 100f)]
float maxAcceleration = 100f, maxAirAcceleration = 10f;
[SerializeField, Range(0f, 10f)]
float jumpHeight = 3f;
[SerializeField, Range(0, 5)]
int maxAirJumps = 1;
[SerializeField]
bool resetAirJumpsOnWallJump;
int jumpPhase;
[SerializeField, Range(0f, 90f)]
float maxGroundAngle = 25f, maxStairsAngle = 50f;
//Setting maxSnapSpeed to the same value as maxSpeed causes inconsistencies... Too bad!
[SerializeField, Range(0f, 100f)]
float maxSnapSpeed = 100f;
[SerializeField, Range(0f, 100f)]
float gravityIncreaseValue = 10f;
Vector3 gravityIncrease;
[SerializeField, Min(0f)]
float probeDistance = 1f;
[SerializeField, Range(0f, 1f)]
float deadZone = 0.1f;
[SerializeField]
LayerMask probeMask = -1, stairsMask = -1;
Vector3 velocity, desiredVelocity, contactNormal, steepNormal;
Rigidbody body;
bool desiredJump;
int groundContactCount, steepContactCount;
bool OnGround => groundContactCount > 0;
bool OnSteep => steepContactCount > 0;
int stepsSinceLastGrounded, stepsSinceLastJump;
float minGroundDotProduct, minStairsDotProduct;
private void OnEnable()
{
inputs.jumpEvent += CheckJumpInput;
inputs.moveEvent += CheckInput;
}
private void OnDisable()
{
inputs.jumpEvent -= CheckJumpInput;
inputs.moveEvent -= CheckInput;
}
void OnValidate()
{
minGroundDotProduct = Mathf.Cos(maxGroundAngle * Mathf.Deg2Rad);
minStairsDotProduct = Mathf.Cos(maxStairsAngle * Mathf.Deg2Rad);
}
void CheckJumpInput()
{
desiredJump = true;
}
void CheckJumpDesired()
{
if (desiredJump)
{
desiredJump = false;
Jump();
}
}
void Jump()
{
Debug.Log("Jump Fired");
Vector3 jumpDirection;
if (OnGround)
{
Debug.Log("JumpOnGround");
jumpDirection = contactNormal;
jumpPhase += 1;
}
else if (OnSteep)
{
Debug.Log("JumpOnSteep");
jumpDirection = steepNormal;
if (resetAirJumpsOnWallJump)
{
jumpPhase = 0;
}
}
else if(maxAirJumps > 0 && jumpPhase 0f)
{
jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f);
}
velocity += jumpDirection * jumpSpeed;
Debug.Log($"Jump Speed = {jumpSpeed}");
Debug.Log($"alignedSpeed = {alignedSpeed}");
}
void UpdateState()
{
stepsSinceLastGrounded += 1;
stepsSinceLastJump += 1;
velocity = body.velocity;
if (OnGround || SnapToGround() || CheckSteepContacts())
{
stepsSinceLastGrounded = 0;
if(stepsSinceLastJump > 1)
{
jumpPhase = 0;
}
if(groundContactCount > 1)
{
contactNormal.Normalize();
}
}
else
{
contactNormal = Vector3.up;
}
}
bool SnapToGround()
{
if(stepsSinceLastGrounded > 1 || stepsSinceLastJump maxSnapSpeed)
{
return false;
}
if (!Physics.Raycast(body.position, Vector3.down, out RaycastHit hit, probeDistance, probeMask))
{
return false;
}
if(hit.normal.y < GetMinDot(hit.collider.gameObject.layer))
{
return false;
}
groundContactCount = 1;
contactNormal = hit.normal;
float dot = Vector3.Dot(velocity, hit.normal);
if (dot > 0f)
{
velocity = (velocity - hit.normal * dot).normalized * speed;
}
return true;
}
private void OnCollisionEnter(Collision collision)
{
EvaluateCollision(collision);
}
void OnCollisionStay(Collision collision)
{
EvaluateCollision(collision);
}
void EvaluateCollision(Collision collision)
{
float minDot = GetMinDot(collision.gameObject.layer);
for(int i = 0; i < collision.contactCount; i++)
{
Vector3 normal = collision.GetContact(i).normal;
if(normal.y >= minDot)
{
groundContactCount += 1;
contactNormal += normal;
}
else if (normal.y > -0.01f)
{
steepContactCount += 1;
steepNormal += normal;
}
}
}
private void Awake()
{
body = GetComponent();
OnValidate();
}
void CheckInput(Vector2 rawMove)
{
Vector2 playerInput;
playerInput = rawMove;
//playerInput.x = Input.GetAxis("Horizontal");
//playerInput.y = Input.GetAxis("Vertical");
if (Mathf.Abs(playerInput.x) < deadZone) playerInput.x = 0f;
if (Mathf.Abs(playerInput.y) < deadZone) playerInput.y = 0f;
playerInput = Vector2.ClampMagnitude(playerInput, 1f);
if (playerInputSpace)
{
Vector3 forward = playerInputSpace.forward;
forward.y = 0f;
forward.Normalize();
Vector3 right = playerInputSpace.right;
right.y = 0f;
right.Normalize();
desiredVelocity = (forward * playerInput.y + right * playerInput.x) * maxSpeed;
}
else
{
desiredVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
}
}
Vector3 ProjectOnContactPlane(Vector3 vector)
{
return vector - contactNormal * Vector3.Dot(vector, contactNormal);
}
void AdjustVelocity()
{
Vector3 xAxis = ProjectOnContactPlane(Vector3.right).normalized;
Vector3 zAxis = ProjectOnContactPlane(Vector3.forward).normalized;
float currentX = Vector3.Dot(velocity, xAxis);
float currentZ = Vector3.Dot(velocity, zAxis);
float acceleration = OnGround ? maxAcceleration : maxAirAcceleration;
float maxSpeedChange = acceleration * Time.deltaTime;
float newX = Mathf.MoveTowards(currentX, desiredVelocity.x, maxSpeedChange);
float newZ = Mathf.MoveTowards(currentZ, desiredVelocity.z, maxSpeedChange);
velocity += xAxis * (newX - currentX) + zAxis * (newZ - currentZ);
}
void ClearState()
{
groundContactCount = steepContactCount = 0;
contactNormal = steepNormal = Vector3.zero;
}
void ColorChange()
{
if (OnGround)
{
GetComponent().material.SetColor("_BaseColor", Color.black);
}
else if (jumpPhase = minGroundDotProduct)
{
groundContactCount = 1;
contactNormal = steepNormal;
return true;
}
}
return false;
}
void IncreaseGravity()
{
Vector3 gravity = new(0f, -gravityIncreaseValue, 0f);
gravityIncrease = gravity;
velocity += gravityIncrease * Time.deltaTime;
}
private void Update()
{
CheckJumpDesired();
}
private void FixedUpdate()
{
UpdateState();
AdjustVelocity();
IncreaseGravity();
ColorChange();
body.velocity = velocity;
ClearState();
}
}
Я отлаживал различные части метода прыжка, и, кажется, работает нормально, только на самом деле не происходит в режиме игры?>
Подробнее здесь:
https://stackoverflow.com/questions/795 ... method-bei
1745575998
Anonymous
Недавно перешел мой проект Unity в новую систему ввода, и моя логика прыжков не работает, несмотря на то, что метод прыжков правильно называется для Debug.logs. любая помощь ?? < /p> [code]using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class RigidbodyMovement : MonoBehaviour { //remember to set player to "Agent" layer (i used layer 8) so that it doesnt just snap to itself [SerializeField] InputManager inputs; [SerializeField] Transform playerInputSpace = default; [SerializeField, Range(0f, 100f)] float maxSpeed = 10f; [SerializeField, Range(0f, 100f)] float maxAcceleration = 100f, maxAirAcceleration = 10f; [SerializeField, Range(0f, 10f)] float jumpHeight = 3f; [SerializeField, Range(0, 5)] int maxAirJumps = 1; [SerializeField] bool resetAirJumpsOnWallJump; int jumpPhase; [SerializeField, Range(0f, 90f)] float maxGroundAngle = 25f, maxStairsAngle = 50f; //Setting maxSnapSpeed to the same value as maxSpeed causes inconsistencies... Too bad! [SerializeField, Range(0f, 100f)] float maxSnapSpeed = 100f; [SerializeField, Range(0f, 100f)] float gravityIncreaseValue = 10f; Vector3 gravityIncrease; [SerializeField, Min(0f)] float probeDistance = 1f; [SerializeField, Range(0f, 1f)] float deadZone = 0.1f; [SerializeField] LayerMask probeMask = -1, stairsMask = -1; Vector3 velocity, desiredVelocity, contactNormal, steepNormal; Rigidbody body; bool desiredJump; int groundContactCount, steepContactCount; bool OnGround => groundContactCount > 0; bool OnSteep => steepContactCount > 0; int stepsSinceLastGrounded, stepsSinceLastJump; float minGroundDotProduct, minStairsDotProduct; private void OnEnable() { inputs.jumpEvent += CheckJumpInput; inputs.moveEvent += CheckInput; } private void OnDisable() { inputs.jumpEvent -= CheckJumpInput; inputs.moveEvent -= CheckInput; } void OnValidate() { minGroundDotProduct = Mathf.Cos(maxGroundAngle * Mathf.Deg2Rad); minStairsDotProduct = Mathf.Cos(maxStairsAngle * Mathf.Deg2Rad); } void CheckJumpInput() { desiredJump = true; } void CheckJumpDesired() { if (desiredJump) { desiredJump = false; Jump(); } } void Jump() { Debug.Log("Jump Fired"); Vector3 jumpDirection; if (OnGround) { Debug.Log("JumpOnGround"); jumpDirection = contactNormal; jumpPhase += 1; } else if (OnSteep) { Debug.Log("JumpOnSteep"); jumpDirection = steepNormal; if (resetAirJumpsOnWallJump) { jumpPhase = 0; } } else if(maxAirJumps > 0 && jumpPhase 0f) { jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f); } velocity += jumpDirection * jumpSpeed; Debug.Log($"Jump Speed = {jumpSpeed}"); Debug.Log($"alignedSpeed = {alignedSpeed}"); } void UpdateState() { stepsSinceLastGrounded += 1; stepsSinceLastJump += 1; velocity = body.velocity; if (OnGround || SnapToGround() || CheckSteepContacts()) { stepsSinceLastGrounded = 0; if(stepsSinceLastJump > 1) { jumpPhase = 0; } if(groundContactCount > 1) { contactNormal.Normalize(); } } else { contactNormal = Vector3.up; } } bool SnapToGround() { if(stepsSinceLastGrounded > 1 || stepsSinceLastJump maxSnapSpeed) { return false; } if (!Physics.Raycast(body.position, Vector3.down, out RaycastHit hit, probeDistance, probeMask)) { return false; } if(hit.normal.y < GetMinDot(hit.collider.gameObject.layer)) { return false; } groundContactCount = 1; contactNormal = hit.normal; float dot = Vector3.Dot(velocity, hit.normal); if (dot > 0f) { velocity = (velocity - hit.normal * dot).normalized * speed; } return true; } private void OnCollisionEnter(Collision collision) { EvaluateCollision(collision); } void OnCollisionStay(Collision collision) { EvaluateCollision(collision); } void EvaluateCollision(Collision collision) { float minDot = GetMinDot(collision.gameObject.layer); for(int i = 0; i < collision.contactCount; i++) { Vector3 normal = collision.GetContact(i).normal; if(normal.y >= minDot) { groundContactCount += 1; contactNormal += normal; } else if (normal.y > -0.01f) { steepContactCount += 1; steepNormal += normal; } } } private void Awake() { body = GetComponent(); OnValidate(); } void CheckInput(Vector2 rawMove) { Vector2 playerInput; playerInput = rawMove; //playerInput.x = Input.GetAxis("Horizontal"); //playerInput.y = Input.GetAxis("Vertical"); if (Mathf.Abs(playerInput.x) < deadZone) playerInput.x = 0f; if (Mathf.Abs(playerInput.y) < deadZone) playerInput.y = 0f; playerInput = Vector2.ClampMagnitude(playerInput, 1f); if (playerInputSpace) { Vector3 forward = playerInputSpace.forward; forward.y = 0f; forward.Normalize(); Vector3 right = playerInputSpace.right; right.y = 0f; right.Normalize(); desiredVelocity = (forward * playerInput.y + right * playerInput.x) * maxSpeed; } else { desiredVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed; } } Vector3 ProjectOnContactPlane(Vector3 vector) { return vector - contactNormal * Vector3.Dot(vector, contactNormal); } void AdjustVelocity() { Vector3 xAxis = ProjectOnContactPlane(Vector3.right).normalized; Vector3 zAxis = ProjectOnContactPlane(Vector3.forward).normalized; float currentX = Vector3.Dot(velocity, xAxis); float currentZ = Vector3.Dot(velocity, zAxis); float acceleration = OnGround ? maxAcceleration : maxAirAcceleration; float maxSpeedChange = acceleration * Time.deltaTime; float newX = Mathf.MoveTowards(currentX, desiredVelocity.x, maxSpeedChange); float newZ = Mathf.MoveTowards(currentZ, desiredVelocity.z, maxSpeedChange); velocity += xAxis * (newX - currentX) + zAxis * (newZ - currentZ); } void ClearState() { groundContactCount = steepContactCount = 0; contactNormal = steepNormal = Vector3.zero; } void ColorChange() { if (OnGround) { GetComponent().material.SetColor("_BaseColor", Color.black); } else if (jumpPhase = minGroundDotProduct) { groundContactCount = 1; contactNormal = steepNormal; return true; } } return false; } void IncreaseGravity() { Vector3 gravity = new(0f, -gravityIncreaseValue, 0f); gravityIncrease = gravity; velocity += gravityIncrease * Time.deltaTime; } private void Update() { CheckJumpDesired(); } private void FixedUpdate() { UpdateState(); AdjustVelocity(); IncreaseGravity(); ColorChange(); body.velocity = velocity; ClearState(); } } [/code] Я отлаживал различные части метода прыжка, и, кажется, работает нормально, только на самом деле не происходит в режиме игры?> Подробнее здесь: [url]https://stackoverflow.com/questions/79592169/jump-method-not-applying-forces-despide-them-being-calculated-and-the-method-bei[/url]