joystick.input ist aufgrund seiner Schutzstufe nicht zugänglich.
unter Verwendung von UnityEngine;
öffentliche Klasse Myplayer: MonoBehaviour
{
Code: Select all
public float MoveSpeed = 3f;
public float smoothRotationTime = 0.25f;
float currentVelocity;
public bool enableMobileInputs = false;
Transform cameraTransform;
float currentSpeed;
float speedVelocity;
public FixedJoystick joystick;
private void Start()
{
cameraTransform = Camera.main.transform;
}
void Update()
{
Vector2 input = Vector2.zero;
if (enableMobileInputs)
{
input = new Vector2(joystick.input.x, joystick.input.y);
}
else
{
input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero)
{
float rotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, rotation, ref currentVelocity, smoothRotationTime);
}
float targetSpeed = MoveSpeed * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedVelocity, 0.1f);
transform.Translate(transform.forward * currentSpeed * Time.deltaTime, Space.World);
}