2D Vector Composite w/ New Unity Input System
Objective:2D Vector Composite w/ New Unity Input System.
In the first I will create new ActionInput Assets called PlayerInputActions,then generate the scrip using this PlayerInputActions assets.Also I will create new script called Player.Then attach player to player gameObject.
Now I will open PlayerInputActions assets and create Action map ,and also create action called movement.
To create Vector2 composite with New Input system,select your action <Action Propery ,here we change action type from “button” to “value”.
Note:A Vector2 composite will enable us to use WSAD key or in gamepad we will use up down left rigth.
To create a Vector2 binding select action <Control type ,expand the drop down and choose Vector2 .Then Select Action right click and choose Add up/down/left/right composite.
Now we select each bindind and listen to key you want to specify,select binding<listen <tap a key and select it.
Here is a screen shot of what a path description from unity document ,its mean which button from which controll is listened ,it can be from keybord ,or game pad.
Now go to the player scrip,create InputActions script reference then we need create instance of it .Then we need to enable Player input map,after that we need to access movement action then perform call back event.
Note: If you are confused with this step I have covered this step in my previous article.
Go to Movement_performed method,to move this object we will use obj/context /whatever you name this .We do this by creating a Vector2 variable and assing it to contex.ReadValue<T>(). Then use transform.Translate to move this object according to our action binding.
But Suleiman how we would know this ReadValue return .This will return the value which is passed at T bracket but ReadValue require a <T> value equal to value type of binding if a binding use Vector 1 we cant pass vactor2.
If we play the editor and press WSAD button will have an issue.our player is not moving smooth ,he move when I press the button .
To solve this we need to delete the perfomed event and method,then create Vector2 handle the assing to _input map then return the value of a keybody using “ReadValue<Vector2>();”
Now we need to use Translate to and passing move varialbe and define a speed number.
Note:We use this way because readValue do not return float number.
To go further lets say I want my player to not move up and down.I will create newVector3 inside tranlsate and put a zero on y then say leave x and y of move to default.
This is it ,I hope you undersand ,if you have question comment below.
See you in the next article.