Answer the question
In order to leave comments, you need to log in
How to set the camera rotation along the Y axis with the character, and along the X axis without?
Good afternoon! I can't figure out how to make the camera look up without rotating the character, if the camera rotates along the y-axis along with the character. Itself in unity not so long ago, it is still difficult to understand and understand some things where there is some kind of mathematics.
This is the script of the character itself, I use two joysticks - one to run, the second to rotate the camera
public class Player : MonoBehaviour
{
public SimpleTouchController leftController;
private float movementSpeed = 7.0f;
private float gravity = 20.0f;
CharacterController character;
Transform cameraTransform;
Vector3 move = Vector3.zero;
private void Start()
{
character = GetComponent<CharacterController>();
cameraTransform = Camera.main.transform;
}
void Update()
{
float horizontal = leftController.GetTouchPosition.x;
float vertical = leftController.GetTouchPosition.y;
if(character.isGrounded)
{
move = new Vector3(horizontal, 0.0f, vertical);
move = cameraTransform.TransformDirection(move);
}
move.y -= gravity * Time.deltaTime;
character.Move(move * movementSpeed * Time.deltaTime);
}
}
public class Camera : MonoBehaviour
{
public GameObject target; // персонаж
public SimpleTouchController rightController;
private float rotateSpeed = 5;
private Vector3 offset;
void Start()
{
offset = target.transform.position - transform.position;
}
void LateUpdate()
{
float horizontal = rightController.GetTouchPosition.x;
float vertical = rightController.GetTouchPosition.y;
target.transform.Rotate(0, horizontal, 0);
float desiredAngle = target.transform.eulerAngles.y; //берем вращение игрока в мировом пространстве
Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0); //поворачиваем на y градусов вокруг оси y
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question