D
D
donvista2022-04-19 19:01:24
C++ / C#
donvista, 2022-04-19 19:01:24

How to fix the rotation of an object or lock the axis of rotation?

https://www.youtube.com/watch?v=WpelcvjmObY
Actually the whole code, everything is very simple. As planned, the object should look at the target, it does everything well, looks at the target. But gradually, as the object moves relative to the target, the Z axis rotates, and I need it to remain the same. I tried to do Euler.z = 0, but all my beautiful rotation immediately deteriorates, in some axes it even starts to turn wildly. How and how should my object be rotated, or is my execution wrong initially?

[SerializeField] private Transform target;
    void Update()
    {
        Vector3 relativePos = (target.position - transform.position).normalized;
        transform.rotation = Quaternion.LookRotation(relativePos, transform.up);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Q
Qerriom Magnelius, 2022-04-19
@MicroProger

Try to lock the axis in RigidBody
(see RigidBody on the screenshot, there is an open tab)
625efb90de028167955155.png

D
donvista, 2022-04-20
@donvista

My partial solution to the problem, the Z axis is always flat, but under certain conditions, a rope or a cube still sharply twists 180 degrees, I still expect better solutions from you

[SerializeField] private Transform target;
    void Update()
    {
        transform.rotation = target.rotation;
        Vector3 relativePos = (target.position - transform.position).normalized;
        transform.rotation = Quaternion.LookRotation(relativePos, transform.up);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question