I just got the new Input System for unity package acouple days before I would've finished the game(Big oof) and I followed a Unity Learn tutorial, but I can't seem to get it to work. Now, I know there are some extra configs you have to make in your project settings and prefrences and I'm pretty sure I got all that correct, so as far as the actual script does this look correct to y'all?
using System.Collections; /*We are using the "using" keyword to signify that we are implementing the Collections class of the System package(because
unity can not use Generic as a baselayer for a script) in this scope/file*/
using System.Collections.Generic; /*We are using the "using" keyword to signify that we are implementing the Generic class of the System.Collections
package(because it contains generic class definition and provides you to better type safly) in this scope/file*/
using UnityEngine; /*We are using the "using" keyword to signify that we are implementing the UnityEngine class(because it allows us to use all of
unity class definitions, keywords, default method and everythine) in this scope/file*/
using UnityEngine.InputSystem;
public class PlayerMovementE5 : MonoBehaviour /*Creates a public(which means it's accesible to every other file) with the name
"PlayerMovementE5" that imports it with the baseclass of "MonoBehaviour"(which means it can be attached to a gameobjct)*/
{
private Rigidbody player;
private float movementX;
private float movementY;
private float movementZ;
public float speed = 100;
void Start() {
player = GetComponent<Rigidbody>();
}
private void OnMove(InputValue movementValue) {
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void FixedUpdate() {
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
player.AddForce(movement * speed);
}
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
The unity's input system script would contain Move on the action maps and MoveLeftRight on the actions. MoveLeftRight's action type will be passthrough and control type will be button. Then create a new binding of type 1d axis. And then create another property under it and then assign your required keys to it Then save and close the input tab and then go to project folder and then click on your input system script file and tick the "generate c# class" and then apply.
Then this code might help:
void FixedUpdate()
{
Movement();
}
private void Movement()
{
speedOfThePlayer = 8;
float move = playerInput.Move.Move.ReadValue<float>();
Vector3 positionNow = transform.position;
positionNow.x = move * speedOfThePlayer;
transform.position = positionNow;
}
Hope it helped you!