- Home /
How to acces or get float work on Parameter using new Input System
im Using Float moveX/moveY, because i want to make a blend animation. if thare is a better way to do it maybe, i need some help because im still bit confused using this new input system
here the CODE
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(Animator))]
public class pInputSystem : MonoBehaviour
{
[Header("Object Reference")]
[SerializeField]
private InputActionReference MovementControl;
[SerializeField]
private InputActionReference JumpControl;
[Header("Variable")]
[Range(0,5)]
[SerializeField]
private float playerSpeed;
[Range(0,5)]
[SerializeField]
private float jumpHeight;
[Range(0, -10)]
[SerializeField]
private float gravityValue;
[Range(0,5)]
[SerializeField]
private float rotationSpeed;
private float moveX , moveY;
private Animator anim;
private CharacterController pCharacter;
private PlayerInput pInput;
private CInput pInputActions;
private Vector3 pVelocity;
private Transform cameraMain;
private bool GroundedPlayer;
private void OnEnable() {
MovementControl.action.Enable();
JumpControl.action.Enable();
pInputActions.Player.Enable();
}
private void OnDisable() {
MovementControl.action.Disable();
JumpControl.action.Disable();
pInputActions.Player.Disable();
}
private void Awake() {
anim = GetComponent<Animator>();
pCharacter = GetComponent<CharacterController>();
pInput = GetComponent<PlayerInput>();
cameraMain = Camera.main.transform;
pInputActions = new CInput();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
pCharacterControler();
pCharacterAnimation();
}
private void FixedUpdate() {
}
private void pCharacterControler(){
GroundedPlayer = pCharacter.isGrounded;
if(GroundedPlayer && pVelocity.y < 0){
pVelocity.y = 0f;
}
Vector2 movement = MovementControl.action.ReadValue<Vector2>();
Vector3 move = new Vector3(movement.x, 0f, movement.y);
move = cameraMain.forward * move.z + cameraMain.transform.right * move.x;
move.y = 0f;
pCharacter.Move(move * Time.deltaTime * playerSpeed );
if (JumpControl.action.triggered && GroundedPlayer){
pVelocity.y += Mathf.Sqrt(jumpHeight * -3f * gravityValue);
}
pVelocity.y += gravityValue * Time.deltaTime;
pCharacter.Move(pVelocity * Time.deltaTime);
if (movement != Vector2.zero){
float targetAngle = Mathf.Atan2(movement.x, movement.y) * Mathf.Rad2Deg + cameraMain.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0f, targetAngle, 0f);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
}
}
private void pCharacterAnimation(){
}
}
Answer by JacobRGames · Mar 14 at 11:51 PM
I don't think I understand what you mean by getting a float from the new input system. You are already getting a vector 2 from the InputActionReference
called MovementControl
. And a vector 2 is just two floats put into one variable. So if you want to get the X or Y axis as a float just access them from the vector 2.
Also, if you want to learn more about the new input system, I would strongly recommend trying out the sample projects that Unity provides in the package manager. You can find the sample projects by looking up the Input System in the package manager under the Unity registry packages. And once you are there, you will see a drop-down list labelled Samples, and there are so many different examples of how to use the new input system.
Thank you for the Respond! im totaly understand what you mean.
im actual problem is how do i make the Parameter to read my Float that im already crated thare could i just make Vec2 float or just a indivual float ?
Vector2 movement = MovementControl.action.ReadValue<Vector2>();
This code right here is reading the input and putting it into a local Vector 2 called movement. Suppose you want to store this value in a variable for other parts of your script to use. You could use either one of these methods that I've shown below. But I would recommend just using a Vector 2.
I'm sorry that I could not post a photo of the code. For some reason, the picture won't upload.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(Animator))]
public class InputSystem : MonoBehaviour
{
[Header("Object Reference")]
[SerializeField]
private InputActionReference MovementControl;
private Vector2 rawInput;
private float inputX, inputY;
private void FixedUpdate()
{
// Storing the input into a Vector 2
rawInput = MovementControl.action.ReadValue<Vector2>();
// Storing the input into 2 floats
inputX = MovementControl.action.ReadValue<Vector2>().x;
inputY = MovementControl.action.ReadValue<Vector2>().y;
}
}
Thank you this is the one im looking for... this is almost the same code that im thinking for couple of hours