so I’m creating a 3D platformer and I have a problem, on the stage there is a model that is raised above the ground, attached to the model: a character controller. movement script and animator, when I run the scene the model moves to a certain position and I can’t move, the buttons also work animations but the player can’t move ( please don’t give me advice about how I could do this through rigedbody, or how I should create an object and make the model a child, or anything like that, I’m specifically asking for advice on solving this problem )
here is the code and video
parameters
public class playerMovement : MonoBehaviour {
public float speed;
public float jumpSpeed;
public float jumpButtonGracePeriod;
private Animator animator;
private CharacterController characterController;
private float ySpeed;
private float originalStepOffset;
private float? lastGroundedTime;
private float? jumpButtonPressedTime;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
originalStepOffset = characterController.stepOffset;
}
// Update is called once per frame
void Update()
{
float horizintalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 movementDirection = new Vector3(horizintalInput, 0, verticalInput);
float magnitude = Mathf.Clamp01(movementDirection.magnitude) * speed;
movementDirection.Normalize();
ySpeed += Physics.gravity.y * Time.deltaTime;
if ( characterController.isGrounded )
{
lastGroundedTime = Time.time;
}
if (Input.GetButtonDown("Jump"))
{
jumpButtonPressedTime = Time.time;
}
if (Time.time - lastGroundedTime <= jumpButtonGracePeriod)
{
characterController.stepOffset = originalStepOffset;
ySpeed = -0.5f;
if (Time.time - jumpButtonPressedTime <= jumpButtonGracePeriod)
{
ySpeed = jumpSpeed;
jumpButtonPressedTime = null;
lastGroundedTime = null;
}
}
else
{
characterController.stepOffset = 0;
}
Vector3 velocity = movementDirection * magnitude;
velocity.y = ySpeed;
characterController.Move(velocity * Time.deltaTime);
if (movementDirection != Vector3.zero)
{
animator.SetBool("isMoving", true);
transform.forward = movementDirection;
}
else
{
animator.SetBool("isMoving", false);
}
}
}