Greetings to all!
There is such a script
using System;
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class Controller3DExample : MonoBehaviour
{
public const float ROTATE_SPEED = 15f;
public float movementSpeed = 5f;
public CNAbstractController MovementJoystick;
private CharacterController _characterController;
private Transform _mainCameraTransform;
private Transform _transformCache;
private Transform _playerTransform;
void Start()
{
// You can also move the character with an event system
// You MUST CHOOSE one method and use ONLY ONE a frame
// If you wan't the event based control, uncomment this line
// MovementJoystick.JoystickMovedEvent += MoveWithEvent;
_characterController = GetComponent<CharacterController>();
_mainCameraTransform = Camera.main.GetComponent<Transform>();
_transformCache = GetComponent<Transform>();
_playerTransform = _transformCache.FindChild("Cocoon");
}
// Update is called once per frame
void Update()
{
var movement = new Vector3(
MovementJoystick.GetAxis("Horizontal"),
0f,
MovementJoystick.GetAxis("Vertical"));
CommonMovementMethod(movement);
}
private void MoveWithEvent(Vector3 inputMovement)
{
var movement = new Vector3(
inputMovement.x,
0f,
inputMovement.y);
CommonMovementMethod(movement);
}
private void CommonMovementMethod(Vector3 movement)
{
movement = _mainCameraTransform.TransformDirection(movement);
movement.y = 0f;
movement.Normalize();
FaceDirection(movement);
_characterController.Move(movement * movementSpeed * Time.deltaTime);
}
public void FaceDirection(Vector3 direction)
{
StopCoroutine("RotateCoroutine");
StartCoroutine("RotateCoroutine", direction);
}
IEnumerator RotateCoroutine(Vector3 direction)
{
if (direction == Vector3.zero) yield break;
Quaternion lookRotation = Quaternion.LookRotation(direction);
do
{
_playerTransform.rotation = Quaternion.Lerp(_playerTransform.rotation, lookRotation, Time.deltaTime * ROTATE_SPEED);
yield return null;
}
while ((direction - _playerTransform.forward).sqrMagnitude > 0.2f);
}
}
I want to convert a 2D game. Change everything Vector3 on Vector2 and removes one variable. It turns out this code
using System;
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class Controller3DExample : MonoBehaviour
{
public const float ROTATE_SPEED = 15f;
public float movementSpeed = 5f;
public CNAbstractController MovementJoystick;
private CharacterController _characterController;
private Transform _mainCameraTransform;
private Transform _transformCache;
private Transform _playerTransform;
void Start()
{
// You can also move the character with an event system
// You MUST CHOOSE one method and use ONLY ONE a frame
// If you wan't the event based control, uncomment this line
// MovementJoystick.JoystickMovedEvent += MoveWithEvent;
_characterController = GetComponent<CharacterController>();
_mainCameraTransform = Camera.main.GetComponent<Transform>();
_transformCache = GetComponent<Transform>();
_playerTransform = _transformCache.FindChild("Cocoon");
}
// Update is called once per frame
void Update()
{
var movement = new Vector2(
MovementJoystick.GetAxis("Horizontal"),
MovementJoystick.GetAxis("Vertical"));
CommonMovementMethod(movement);
}
private void MoveWithEvent(Vector2 inputMovement)
{
var movement = new Vector2(
inputMovement.x,
inputMovement.y);
CommonMovementMethod(movement);
}
private void CommonMovementMethod(Vector2 movement)
{
movement = _mainCameraTransform.TransformDirection(movement);
movement.Normalize();
FaceDirection(movement);
_characterController.Move(movement * movementSpeed * Time.deltaTime);
}
public void FaceDirection(Vector2 direction)
{
StopCoroutine("RotateCoroutine");
StartCoroutine("RotateCoroutine", direction);
}
IEnumerator RotateCoroutine(Vector2 direction)
{
if (direction == Vector2.zero) yield break;
Quaternion lookRotation = Quaternion.LookRotation(direction);
do
{
_playerTransform.rotation = Quaternion.Lerp(_playerTransform.rotation, lookRotation, Time.deltaTime * ROTATE_SPEED);
yield return null;
}
while ((direction - _playerTransform.forward).sqrMagnitude > 0.2f);
}
}
As a result of an error in this line
while ((direction - _playerTransform.forward).sqrMagnitude > 0.2f);