Use the handle to control the movement of the character in the third person in Unity
This article recommends viewing in conjunction with this article:
https://blog.csdn.net/qq_45919090/article/details/109393562
Unity2017 and above versions will automatically recognize the handle when inserting the handle. The default input method and button configuration can be found in the Inspector panel in Edit–>Project Settings–>Input and can be customized and modified. In order to interact with the keyboard and To distinguish the input mode of the mouse, we do the following settings:
Left stick:
Right joystick:
Cross key:
LT and RT:
If the key is not clear about its code, you can use the following script to detect the key:
public class GetKeyValue : MonoBehaviour {
public float speed = 10.0F;
public float rotationSpeed = 100.0F;
void Update()
{
detectPressedKeyOrButton();
}
public void detectPressedKeyOrButton()
{
foreach (KeyCode kcode in System.Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(kcode))
Debug.Log("KeyCode down: " + kcode);
}
}
}
You can get:
button | Codename | button | Codename |
---|---|---|---|
A | JoystickButton0 | RB | JoystickButton5 |
B | JoystickButton1 | Back | JoystickButton6 |
X | JoystickButton2 | Start | JoystickButton 7 |
Y | JoystickButton3 | Press the left stick | JoystickButton8 |
LB | JoystickButton4 | Right joystick press | JoystickButton9 |
Realize that the character moves in the direction of the joystick
(Note: The binding object of the following script is the game character, Charactor)
For the character Charactor in the scene, if you want to change the direction of its movement, you need to change its rotation angle, here you can use the local Euler angle LocalEulerAngles. However, if you directly assign the vector (L_H, 0, L_V) returned by the joystick to the charactor.forward, the direction will be confused after the lens is rotated. The method used here is to create an empty object forward, making forwa.position equal to charactor.position, Then assign the vector returned by the handle to the forward direction.
In the post released at the beginning of the article, you can get the two parent objects Empty and Position used to control the camera. In order to achieve rotation, you can do the following settings.
Define a method to make the position of forward equal to the position of the character, make the forward direction of forward equal to the vector of the handle, and finally make the character's LocalEulerAngles equal to the local Euler angle of empty plus the local Euler angle of forward
private void ApplyRotate()
{
forward.position = transform.position;
//forward.forward = new Vector3(L_H, 0, L_V);
forward.forward = Vector3.Lerp(forward.forward, new Vector3(L_H, 0, L_V), 0.3f);
transform.localEulerAngles = empty.localEulerAngles + forward.localEulerAngles;
}
Then get the handle vector to move the character, and do the following settings in the character's state machine:
The method is not unique, the switching conditions can be set according to your own animation, or you can directly use the hybrid tree to calculate
The Move method is as follows, and the wording is not unique, here is to allow the character to switch between walking and running when the left stick of the handle is pressed
void Move()
{
if (Mathf.Abs(L_H) > 0.02f || Mathf.Abs(L_V) > 0.02f)
{
if (Input.GetKeyDown(KeyCode.JoystickButton8))
{
if (anim.GetFloat("Speed") < 2)
{
anim.SetFloat("Speed", 5);
ApplyRotate();
shift = true;
anim.SetBool("IsShift", shift);
}
else if (anim.GetFloat("Speed") > 4.8f)
{
anim.SetFloat("Speed", 1.4f);
ApplyRotate();
shift = false;
anim.SetBool("IsShift", shift);
}
}
else
{
if (anim.GetFloat("Speed") > 4.8f)
{
anim.SetFloat("Speed", 5);
ApplyRotate();
shift = true;
anim.SetBool("IsShift", shift);
}
else if (anim.GetFloat("Speed") < 2)
{
anim.SetFloat("Speed", 1.4f);
ApplyRotate();
shift = false;
anim.SetBool("IsShift", shift);
}
}
}
else
{
anim.SetFloat("Speed", 0);
}
}
Just call the method in Update
void Update()
{
L_H = Input.GetAxis("Horizontal_Left");
L_V = Input.GetAxis("Vertical_Left");
shift = false;
Move();
}