P
P
PsyDuckbest2021-08-05 14:50:39
C++ / C#
PsyDuckbest, 2021-08-05 14:50:39

How to fix the camera on a rotating object?

Hello! My character rotates in four directions instantly using Quaternion.Euler, but the camera rotates along with it, which should be static. So I'm thinking, is it possible to somehow fix this without crutches like four cameras that will turn on depending on the direction of the character?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NoNameDeveloper, 2021-08-05
@PsyDuckbest

I already answered in the comments to another question, but I'll leave it for others to understand if they encounter this.
The camera must not be a child of an object that is being rotated.
Set the camera rotation manually, and the distance in the inspector (offset)

using UnityEngine;

// Кинуть на камеру
// Не забывать менять вращение самой камеры в ручную на сколько нужно.
public class CameraFollow : MonoBehavior
{
  // Соединить тут объект которого камера должна преследовать.
  [SerializedField] private Transform _target;

  // Дистанция между объекта и самой камеры.
  [SerializedField] private Vector3 _offset = new Vector3(0, -10, 0);

  // Насколько плавно камера будет следовать за объектом.
  [SerializedField] private float _smooth = .1f

  // Methods

  private void Update()
  {
    transform.position = Vector2.Lerp(transform.position, _target.transform.position + _offset, _smooth);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question