Hello everyone, I need help with this script,
context: this is a character control script that I made from this tutorial
, I wanted a first person controller so I used another tutorial to make the camera movement that’s a separate script.
It uses the new input.
This script is attached to a character model along with built in character controller, rigid body, capsule and the camera movement script.
The problem is when I reached a stage of implementing the ladder climbing feature I started seeing problems I tried many tutorials to create my ladder feature but all did not work, even chatGPT couldn’t help, I when the assigned key for climb up is pressed character only tries to move, it moves an inch I think gravity is causing the problem but I don’t know how to fix it, I scrapped off the codes that were handling ladder system, I used the tutorials that come first when you search unity ladder climbing on YouTube, include codemonkey his code wasn’t working, I tried using alternative method of ladder climbing like teleporting but it didn’t work out aswell, I am still a beginner.
Here is the script I will appreciate the help
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Script
{
public class PlayerControls : MonoBehaviour
{
[Header("MMOVEMENT PARAMETERS")]
[SerializeField] private CharacterController controller;
[SerializeField] private Vector3 playerVelocity;
[SerializeField, Range(0, 5)] private float jumpHeight = 1.0f;
[SerializeField] private float gravityValue = -9.81f;
public Transform player;
//Animations
public Animator Anim;
private float horizontalInput;
private float verticalInput;
[Range(-1, -10)] public int fallHeight;
public PlayerJoystick playerInput;
public Transform cameraTransform;
private float speedSmoothVelocity;
private float currentSpeed;
private Vector3 currentVelocity;
//Check velocity
[Space(10)]
[Header("VELOCITY CHECK")]
public Text VelocityText;
public Rigidbody playerRigidbody;
[Space(10)]
[Header("SPRINTING PARAMETERS")]
//Sprinting
public Image sprintButton;
public Color On;
public Color Off;
[Space(10)]
[Header("CROUCH PARAMETERS")]
public float crouchHeight;
public float crouchCenter;
public float OriginalHeight;
public float OriginalCenter;
public Image crouchButton;
[SerializeField] private GameObject infoTextObject;
[ExecuteAlways, Range(0, 5)] public float infoDuration;
[Space(10)]
[Header("LADDER CLIMB PARAMETERS")]
[SerializeField] private bool climbingLadder;
[SerializeField] private float LadderSpeed;
[SerializeField] private GameObject ladderButtons;
[SerializeField] Button UpArrow;
[SerializeField] Button DownArrow;
[SerializeField] GameObject Joystick;
[SerializeField] Rigidbody rb;
public CharacterController box;
[Space(10)]
[Header("SPEED PARAMETERS")]
[SerializeField, ExecuteAlways, Range(0, 10)] private float playerSpeed;
[SerializeField, ExecuteAlways, Range(0, 10)] private float sprintSpeed;
[SerializeField, ExecuteAlways, Range(0, 10)] private float crouchSpeed;
[SerializeField, ExecuteAlways, Range(0, 10)] private float walkSpeed;
private Text speedText;
[Space(10)]
[Header("ACTIONS PARAMETERS")]
[SerializeField] private bool canJump = true;
[SerializeField] private bool isCrouching;
[SerializeField] private bool isSprinting;
[SerializeField] private bool groundedPlayer;
private void Awake()
{
playerInput = new PlayerJoystick();
controller = GetComponent<CharacterController>();
Anim = GetComponentInChildren<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
VelocityText = GameObject.Find("Velocity Text").GetComponent<Text>();
speedText = GameObject.Find("Speed Text").GetComponent<Text>();
infoTextObject = GameObject.Find("Info Text");
sprintButton = GameObject.Find("Sprint").GetComponent<Image>();
crouchButton = GameObject.Find("Crouch").GetComponent<Image>();
//ladderButtons = GameObject.Find("Ladder UI");
//UpArrow = GameObject.Find("Up").GetComponent<Button>();
//DownArrow = GameObject.Find("Down").GetComponent<Button>();
//Joystick = GameObject.Find("Joystick");
Joystick.SetActive(true);
infoTextObject.SetActive(false);
ladderButtons.SetActive(false);
}
private void OnEnable()
{
playerInput.Enable();
}
private void OnDisable()
{
playerInput.Disable();
}
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
// Get the movement input vector
Vector2 movementInput = playerInput.PlayerMainMob.Move.ReadValue<Vector2>();
Vector3 move = new Vector3(movementInput.x, 0f, movementInput.y);
// Transform the move direction to be relative to the camera's rotation
move = Quaternion.Euler(0, cameraTransform.eulerAngles.y, 0) * move;
float targetSpeed = movementInput.magnitude * playerSpeed;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, 0.1f);
//move the player
controller.Move(move * currentSpeed * Time.deltaTime);
// Update the animator parameters
horizontalInput = movementInput.x;
verticalInput = movementInput.y;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///INPUTS///
// Input for Jump
if (playerInput.PlayerMainMob.Jump.triggered)
{
if (canJump)
{
Anim.SetTrigger("Jump");
playerVelocity.y += Mathf.Sqrt(jumpHeight * -2f * gravityValue);
}
else if (!canJump)
{
infoTextObject.SetActive(true);
Invoke("DeActivate", infoDuration);
}
}
//Check if the player is falling and transition to the appropriate animation blend trees
if (playerVelocity.y < fallHeight)
{
Anim.SetBool("Falling", true);
}
else if (playerVelocity.y > -1 && !climbingLadder)
{
Anim.SetBool("Falling", false);
}
if (!climbingLadder)
{
// Apply gravity to the player
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
//Check Velocity
Vector3 velocity = playerRigidbody.velocity;
string velocityString = "Velocity: " + velocity.magnitude.ToString("F2");
VelocityText.text = velocityString;
//Check player speed
speedText.text = "Speed: " + playerSpeed.ToString();
//Sprinting input
if (playerInput.PlayerMainMob.Sprint.triggered)
{
//set bool
if (!isSprinting)
{
isSprinting = true;
// Play animation
Anim.SetBool("Sprinting", true);
// Set the animator parameters to control the sprint animations
Anim.SetFloat("Horizontal", horizontalInput);
Anim.SetFloat("Vertical", verticalInput);
// Button color
sprintButton.color = On;
// Increase player speed
playerSpeed = sprintSpeed;
}
else if (isSprinting)
{
//set bool
isSprinting = false;
// Turn off animation
Anim.SetBool("Sprinting", false);
// Change color
sprintButton.color = Off;
if (isCrouching)
{
playerSpeed = crouchSpeed;
// Set the animator to control the crouch animations
Anim.SetFloat("Xdirection", horizontalInput);
Anim.SetFloat("Ydirection", verticalInput);
}
else
{
playerSpeed = walkSpeed;
// Set the animator parameters to control the walk animations
Anim.SetFloat("x", horizontalInput);
Anim.SetFloat("y", verticalInput);
}
}
//end of sprint check
}
else if (playerInput.PlayerMainMob.Sprint.triggered)
{
//Cancel crouching
isCrouching = false;
// Turn off animation
Anim.SetBool("Crouching", false);
// Change color
crouchButton.color = Off;
// Reset the height and center
controller.height = OriginalHeight;
controller.center = new Vector3(0, OriginalCenter, 0);
///////////////////////////////////////////////
//Activate crouching
//set bool
isSprinting = true;
// Play animation
Anim.SetBool("Sprinting", true);
// Set the animator parameters to control the sprint animations
Anim.SetFloat("Horizontal", horizontalInput);
Anim.SetFloat("Vertical", verticalInput);
// Button color
sprintButton.color = On;
// Increase player speed
playerSpeed = sprintSpeed;
}
//Crouch input
if (playerInput.PlayerMainMob.Crouch.triggered)
{
//check if crouching already
if (!isCrouching)
{
//set bool
isCrouching = true;
// Play animation
Anim.SetBool("Crouching", true);
// Set the animator to control the crouch animations
Anim.SetFloat("Xdirection", horizontalInput);
Anim.SetFloat("Ydirection", verticalInput);
// Change color
crouchButton.color = On;
// Set the new height and center
controller.height = crouchHeight;
controller.center = new Vector3(0, crouchCenter, 0);
// Change player speed
playerSpeed = crouchSpeed;
}
else if (isCrouching)
{
//set bool to not crouching
isCrouching = false;
// Turn off animation
Anim.SetBool("Crouching", false);
// Change color
crouchButton.color = Off;
// Reset the height and center
controller.height = OriginalHeight;
controller.center = new Vector3(0, OriginalCenter, 0);
//change speed
if (isSprinting)
{
playerSpeed = sprintSpeed;
// Set the animator parameters to control the sprint animations
Anim.SetFloat("Horizontal", horizontalInput);
Anim.SetFloat("Vertical", verticalInput);
}
else
{
playerSpeed = walkSpeed;
// Set the animator parameters to control the walk animations
Anim.SetFloat("x", horizontalInput);
Anim.SetFloat("y", verticalInput);
}
}
}
else if (playerInput.PlayerMainMob.Crouch.triggered && isSprinting)
{
/// cancel sprinting
//set bool
isSprinting = false;
// Turn off animation
Anim.SetBool("Sprinting", false);
// Change color
sprintButton.color = Off;
//Change player speed
playerSpeed = walkSpeed;
/////////
//activate crouching
//set bool
isCrouching = true;
// Set the animator to control the crouch animations
Anim.SetFloat("Xdirection", horizontalInput);
Anim.SetFloat("Ydirection", verticalInput);
// Play animation
Anim.SetBool("Crouching", true);
// Change color
crouchButton.color = On;
// Set the new height and center
controller.height = crouchHeight;
controller.center = new Vector3(0, crouchCenter, 0);
// Change player speed
playerSpeed = crouchSpeed;
}
//if player is not sprinting or crouching
if (!isSprinting && !isCrouching)
{
playerSpeed = walkSpeed;
// Set the animator parameters to control the walk animations
Anim.SetFloat("x", horizontalInput);
Anim.SetFloat("y", verticalInput);
}
//play animation controls
if (isSprinting)
{
// Turn off animation
Anim.SetBool("Sprinting", true);
// Set the animator parameters to control the sprint animations
Anim.SetFloat("Horizontal", horizontalInput);
Anim.SetFloat("Vertical", verticalInput);
}
if (isCrouching)
{
// Play animation
Anim.SetBool("Crouching", true);
// Set the animator to control the crouch animations
Anim.SetFloat("Xdirection", horizontalInput);
Anim.SetFloat("Ydirection", verticalInput);
}
if (isCrouching)
{
canJump = false;
}
else if (!isCrouching)
{
canJump = true;
}
//Ladder////
}
void DeActivate()
{
infoTextObject.SetActive(false);
}
}
}