StrannikY
@StrannikY
Программирую на C#, часто загораюсь идеями,учусь.

Как в unity сделать плавный поворот дочернего объекта?

Пишу код для игры.В ней есть метла,которую нужно двигать вправо-влево нажатием кнопок.Но мне ещё надо поворачивать при этом родительский объект,а так как я использую Quaternion.Euler ,то 0 градусов относительно оси y не поворачиваются вокруг самого объекта,а поворачиваются вокруг мировой оси y.

Если короче: Персонаж поворачивается,метла поворачивается относительно его(то есть перемещается),но относительно мира при нажатии кнопки "e" она выравнивается по оси y

Вот код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BroomController : MonoBehaviour
{
    private GameObject broom;
    private bool InHand = true;
    // Start is called before the first frame update
    void Start()
    {
        broom = GameObject.Find("Broom");
        Cursor.visible = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("q") || Input.GetKey("e"))
        {
            if (Input.GetKey("q"))
            {
                Quaternion fromRT = broom.transform.rotation;
                Quaternion toRT = Quaternion.Euler(0, 0, 20);
                broom.transform.rotation = Quaternion.Lerp(fromRT, toRT, Time.deltaTime * 10f);
            }
            if (Input.GetKey("e"))
            {
                Quaternion fromRT = broom.transform.rotation;
                Quaternion toRT = Quaternion.Euler(0, 0, -20);
                broom.transform.rotation = Quaternion.Lerp(fromRT, toRT, Time.deltaTime * 10f);
            }
        }
    }
}
  • Вопрос задан
  • 60 просмотров
Пригласить эксперта
Ответы на вопрос 1
StrannikY
@StrannikY Автор вопроса
Программирую на C#, часто загораюсь идеями,учусь.
Спасибо Lapita12 за ответ:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BroomController : MonoBehaviour 
{
    private GameObject broom;
    private bool InHand = true;

    
    void Start() 
    {
        broom = GameObject.Find("Broom");
        Cursor.visible = false;
    }

    
    void Update() 
    {
        GameObject parent = broom.transform.parent.gameObject;
        float targetAngle = 0f;

        if (Input.GetKey("q") || Input.GetKey("e")) 
        {
            if (Input.GetKey("q")) 
            {
                targetAngle = 20f;
            }

            if (Input.GetKey("e")) 
            {
                targetAngle = -20f;
            }
            
            Quaternion parentRotation = parent.transform.rotation;
            Quaternion targetRotation = Quaternion.Euler(0, 0, targetAngle);
            Quaternion broomTargetRotation = parentRotation * targetRotation;
            broom.transform.rotation = Quaternion.RotateTowards( broom.transform.rotation, broomTargetRotation, Time.deltaTime * 180f);
        }
    }
}
Ответ написан
Комментировать
Ваш ответ на вопрос

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

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