@EugeniuszSZ

Почему камера теряет объект?

Мой объект, вместе с камерой, меняет вектор передвижения при нажатии кнопки. Мне нужно, чтобы камера меняла позицию до передвижения, но все работает только, когда объект стоит на месте, иначе камера теряет его. (центр камеры при нажатии должен переходить на расстояние двух векторов (вектор равен от центра камеры до объекта по оси x))
Когда нет движения все работает: 628a800b8338c084597939.png
628a8018756b8338772445.png
После передвижения и поворота:
628a80e86537d662572290.png
628a80f1d2aa2320716403.png

ic class BSMoving : MonoBehaviour
{
    public GameObject person;

    public Vector3 camdirection;
    public Vector3 moveVector = Vector3.right;
    public float speed = 30f;
    public bool inmoving;
    public bool inmovingR = true;

    void Start()
    {
        camdirection = new Vector3(2*(person.transform.position.x - GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.position.x), GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.position.y, GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.position.z);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            inmoving = !inmoving;
        }
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.position = camdirection;
            camdirection = new Vector3(2*person.transform.position.x - camdirection.x, camdirection.y, camdirection.z);
            inmovingR = !inmovingR;
        }
        if (inmovingR && inmoving)
        { 
          person.transform.Translate(moveVector * speed * Time.deltaTime);
        }

        if (!inmovingR && inmoving)
        {
            person.transform.Translate(-moveVector * speed * Time.deltaTime);
        }
    }

}
  • Вопрос задан
  • 140 просмотров
Решения вопроса 1
@EugeniuszSZ Автор вопроса
Спасибо, вопрос решен - нужно было использовать локальные координаты объекта.
Неправильно:
camdirection = new Vector3(GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.position.x, GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.position.y, GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.position.z);
            
            GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.position = new Vector3(-camdirection.x, camdirection.y, camdirection.z);

Как надо:
camdirection = new Vector3(GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.localPosition.x, GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.localPosition.y, GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.localPosition.z);
            
            GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().transform.localPosition = new Vector3(-camdirection.x, camdirection.y, camdirection.z);
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы