Rookie coder here! Making my first controller script in C#. Using the new input system and the character controller instead of rigidbody.
Making a 3d game, expecting that I should be able to run around on X and Z axis with Y as my height/jump. However when I hit play I’m only able to move on my X axis perfectly fine, but no Z axis.
Either I’m missing something in the code or I’m misunderstanding something I have already written, in either scenario could somebody point me in the right direction?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[SerializeField]
public float speed = 7;
public float jumpHeight = 1.0f;
public float gravityValue = -9.81f;
private bool isGrounded;
private PlayerControls playerControls;
private Vector2 movement;
private Animator animator;
private CharacterController controller;
private Vector3 playerVelocity;
private void Awake()
{
animator = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
playerControls = new PlayerControls();
playerControls.Player.Move.performed += ctx => movement = ctx.ReadValue<Vector2>();
}
private void OnEnable()
{
playerControls.Player.Enable();
playerControls.Player.Move.Enable();
playerControls.Player.Roll.performed += DoRoll;
playerControls.Player.Roll.Enable();
}
private void DoRoll(InputAction.CallbackContext obj)
{
Debug.Log("Roll!");
}
private void OnDisable()
{
playerControls.Player.Disable();
playerControls.Player.Move.Disable();
playerControls.Player.Roll.Disable();
}
private void FixedUpdate()
{
isGrounded = controller.isGrounded;
if (isGrounded && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
float x = movement.x;
float y = movement.y;
animator.SetFloat("inputX", x);
animator.SetFloat("inputY", y);
movement = playerControls.Player.Move.ReadValue<Vector2>();
movement.y = 0f;
playerVelocity = movement * speed;
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}