D
D
Dmitry Korolev2016-07-08 19:11:03
C++ / C#
Dmitry Korolev, 2016-07-08 19:11:03

How to rotate an object knowing the degree of another object?

I need to synchronize the rotations of objects, but I don’t know how to do it)
I already learned aulerangles

using UnityEngine;
using System.Collections;

public class RotationFollower : MonoBehaviour 
{
  public GameObject target;
  public GameObject follower;
  public float rot;
  void Update()
  {
    rot = target.transform.rotation.eulerAngles.y;

  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2016-07-08
@adressmoeistranici

The easiest option is to apply a one-to-one rotation.
You can also take only the rotation around the Y axis, but the result can be unpredictable if the target will rotate around the other two axes.
It's safe enough to calculate the rotation around the Y axis by taking Transform.forward and projecting it onto the XZ plane.

Vector3 flatForward = target.transform.forward;
flatForward.y = 0;
if (flatForward != Vector3.zero)
{
    flatForward.Normalize();
    follower.transform.rotation = Quaternion.LookRotation(flatForward);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question