- Home /
Can only move character left or right, no foward or backwards.
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);
}
}
I'm also new to Unity and scripting. Shouldn't Line 68 be float z = movement.z ? The following that line of reasoning, line 71 be "inputZ", z . I'm guessing line 77 is correct with the .y velocity since its to keep you on the ground.
Nowhere in the code does z seem to come into effect.
Answer by Ronelo · Feb 06 at 11:16 AM
Try to change : “float y = movement.y;”.
To : “float y = movement.z;”.
Unfortunately this didn't work for me. Had tried it before, but from my current understanding the WASD and joystick are both vector2 inputs so there is no z input for it to read.
Not sure with joystick usage. I use the basic UnityEngine then wrote this code if ((Input.GetKey(KeyCode.UpArrow)) || (Input.GetKey(KeyCode.W))) { transform.position += Vector3.forward Time.deltaTime Speed; }
So up arrow and W moves forward along z axis. this character only faces forward. Then used LeftArrow/A for vector3.left, DownArrow/S for vector3.back, and RightArrow/D for vector3.right.
In my 3D platform, this makes my character move decently well.