0

I would like MyCharacter.cs script to move my 3D character forward, backwards, left, and right. I am using Rewired, an advanced input system for Unity.

It works, but rather moving my character forward and backwards, it moves them up and down. I'm still understanding how to access the y, x, and z axis in relation to the code. I thought that:

moveVector.x = player.GetAxis("Horizontal"); // get input by name or action id
moveVector.y = player.GetAxis("Vertical");

Would allow me to go forward and backwards, not up and down.

enter image description here

Here is the code:

// MyCharacter.cs - how to get input from Rewired.Player

using UnityEngine;
using System.Collections;
using Rewired;
//using AC;

[RequireComponent(typeof(CharacterController))]

public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
// The Rewired player id of this character
public int playerId = 0;

// The movement speed of this character
public float moveSpeed = 3.0f;

public float jumpForce;
public float impactVelocity;

// The bullet speed
public float bulletSpeed = 15.0f;

// Assign a prefab to this in the inspector.
// The prefab must have a Rigidbody componet on it in order to work.

public GameObject bulletPrefab;

private Player player; // The Rewired Player
private CharacterController cc;
private Vector3 moveVector;
private bool fire;

private void Awake()
{
    //Get the Rewired Player object for this player and keep it for the duration of the character's lifetime
    player = ReInput.players.GetPlayer(playerId);

    // Get the character controller
    cc = GetComponent<CharacterController>();
}

void Update()
{
    GetInput();
    ProcessInput();
}

private void GetInput()
{
    // Get the input from the Rewired Player. All controllers that the Player owns will contribute, so it doesn't matter
    // whetther the input is coming from a joystick, the keyboard, mouse, or a custom controller.

    moveVector.x = player.GetAxis("Horizontal"); // get input by name or action id
    moveVector.y = player.GetAxis("Vertical");
    fire = player.GetButtonDown("Fire");
}

private void ProcessInput()
{
    // Process movement
    if(moveVector.x != 0.0f || moveVector.y != 0.0f)
    {
        cc.Move(moveVector * moveSpeed * Time.deltaTime);
    }

    // Process fire
    if (fire)
    {
        GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.position + transform.right, transform.rotation);
        bullet.GetComponent<Rigidbody>().AddForce(transform.right * bulletSpeed, ForceMode.VelocityChange);
    }

    // Process jump
    if(player.GetButtonDown("Jump"))
    {
        rb.AddForce(Vector3.up * jumpForce);
    }
}

}

1
  • Please use the correct tags! Note that [unityscript] is or better was a custom JavaScript flavor-like language used in early Unity versions and is long deprecated by now. Your code is of course in c# Commented Aug 25, 2022 at 14:01

1 Answer 1

1

Use Z coordinate instead Y

moveVector.x = player.GetAxis("Horizontal");
moveVector.z = player.GetAxis("Vertical"); // here
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, didn't work.
Have you edit here if(moveVector.x != 0.0f || moveVector.y != 0.0f) in ProcessInput()?
I added to that section! I overlooked that bit, thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.