There is the Movement script:
public const float ROTATE_SPEED = 15f;
public float movementSpeed = 5f;
public Joystick MovementJoystick;
private CharacterController _characterController;
private Transform _mainCameraTransform;
void Awake()
{
_characterController = GetComponent<CharacterController>();
_mainCameraTransform = Camera.main.GetComponent<Transform>();
}
void Update()
{
var movement = _mainCameraTransform.TransformDirection (MovementJoystick.GetAxis ("Horizontal"),
0f,
MovementJoystick.GetAxis ("Vertical"));
movement += Physics.gravity;
movement *= Time.deltaTime;
_characterController.Move (movement * movementSpeed);
}
}
And the camera code:
public Joystick RotateJoystick;
public float RotationSpeed = 10f;
private Transform _transformCache;
private Transform _parentTransformCache;
float rotationX;
float rotationY;
void Awake()
{
_transformCache = GetComponent<Transform>();
_parentTransformCache = _transformCache.parent;
}
void LateUpdate()
{
if (RotateJoystick != null)
{
rotationY -= RotateJoystick.GetAxis("Vertical") * RotationSpeed * Time.smoothDeltaTime;
rotationY = Mathf.Clamp(rotationY, -20, 40);
rotationX += RotateJoystick.GetAxis("Horizontal") * RotationSpeed * Time.smoothDeltaTime;
rotationX = rotationX % 360;
_parentTransformCache.localEulerAngles = new Vector3(rotationY, rotationX);
}
}
Thanks ![]()