Ich habe diese Tutorials verfolgt, die ich den Link für weiter nach unten zur Verfügung stellen werde, und sie haben einige Probleme, die ich bei Kommentaren irgendwie behoben habe. Sie sind nicht so alt, aber alt genug, um einen veralteten Code zu verwenden. Der Code versucht, das Verhalten des Spielers zu ändern, basierend darauf, ob er an einer Steigung steht. Aus irgendeinem Grund funktioniert der Code, während ich meinen Testhang nach oben gehe, aber wenn ich den Hang hinunter gehe, gehe ich stattdessen in die Luft und werde dann durch die Schwerkraft nach unten gezogen, was eine holprige Bewegung erzeugt. IT. < /p>
Sie finden mein MovingPlayer -Skript hier: < /p>
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
private float moveSpeed;
public float walkSpeed;
public float sprintSpeed;
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;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
private Rigidbody rb;
public MovementState state;
public enum MovementState
{
walking,
sprinting,
crouching,
air
}
private void Start()
{
rb = GetComponent();
rb.freezeRotation = true;
readyToJump = true;
startYScale = transform.localScale.y;
}
private void Update()
{
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
MyInput();
SpeedControl();
StateHandler();
// Unity 6: linearDamping statt drag
rb.linearDamping = grounded ? groundDrag : 0f;
if (OnSlope())
{
Debug.Log("OnSlope | normal: " + slopeHit.normal + " | angle: " + Vector3.Angle(Vector3.up, slopeHit.normal));
}
else if (grounded)
{
Debug.Log("Grounded on flat");
}
else
{
Debug.Log("Airborne");
}
}
private void FixedUpdate()
{
MovePlayer();
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
if (Input.GetKey(jumpKey) && readyToJump && grounded)
{
readyToJump = false;
Jump();
Invoke(nameof(ResetJump), jumpCooldown);
}
if (Input.GetKeyDown(crouchKey))
{
transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
}
if (Input.GetKeyUp(crouchKey))
{
transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
}
}
private void StateHandler()
{
if (Input.GetKey(crouchKey))
{
state = MovementState.crouching;
moveSpeed = crouchSpeed;
}
else if (grounded && Input.GetKey(sprintKey))
{
state = MovementState.sprinting;
moveSpeed = sprintSpeed;
}
else if (grounded)
{
state = MovementState.walking;
moveSpeed = walkSpeed;
}
else
{
state = MovementState.air;
}
}
private void MovePlayer()
{
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
if (OnSlope() && !exitingSlope)
{
rb.AddForce(GetSlopeMoveDirection() * moveSpeed * 20f, ForceMode.Force);
}
else if (grounded)
{
rb.AddForce(moveDirection * moveSpeed * 10f, ForceMode.Force);
}
else
{
rb.AddForce(moveDirection * moveSpeed * 10f * airMultiplier, ForceMode.Force);
}
}
private void SpeedControl()
{
if (OnSlope() && !exitingSlope)
{
if (rb.linearVelocity.magnitude > moveSpeed)
{
rb.linearVelocity = rb.linearVelocity.normalized * moveSpeed;
}
}
else
{
Vector3 flatVel = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
if (flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.linearVelocity = new Vector3(limitedVel.x, rb.linearVelocity.y, limitedVel.z);
}
}
}
private void Jump()
{
exitingSlope = true;
rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
rb.AddForce(Vector3.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;
}
}
< /code>
Ich habe auch nur 3 Skript in diesem Projekt, die anderen beiden sind für Kamerabewegungen und Platzierung. Sie sind also wahrscheinlich kein Problem, aber ich werde sie Ihnen auch hier zur Verfügung stellen: < /p>
using UnityEngine;
public class PlayerCam : MonoBehaviour
{
public float sensX;
public float sensY;
public Transform orientation;
float xRotation;
float yRotation;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
private void Update()
{
float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;
yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
orientation.rotation = Quaternion.Euler(0, yRotation, 0);
}
}
< /code>
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
public Transform cameraPosition;
private void Update()
{
transform.position = cameraPosition.position;
}
}
< /code>
Also, here are the mentioned tutorials:
The first one is the one I have trouble with.
Thank you for incoming help!
Einheit 3D Slope -Bewegung erzeugt holprige Bewegung ⇐ C#
-
- Similar Topics
- Replies
- Views
- Last post