E
E
electricalmuffin2018-11-10 17:57:31
C++ / C#
electricalmuffin, 2018-11-10 17:57:31

How can you change the rotation of an object in Unity3D?

Good evening. I would like to know how you can make the rotation of an object along the Y axis equal to the rotation along the Z axis of its parent.
Parent
└ Object

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
theinsaneone, 2018-11-10
@theinsaneone

Call this code on a child object:

var ownLocalAngles = transform.localEulerAngles;
ownLocalAngles.y = transform.parent.eulerAngles.z;
transform.localEulerAngles = ownLocalAngles;

Note that this works with object Euler angles, not quaternions.

M
MishkaVNorky, 2018-11-13
@MishkaVNorky

Look in this direction, I used this implementation on the head of the tank (the main condition was to rotate the head locally, according to the global camera coordinates), so that it would rotate correctly. The muzzle looks where the camera is looking, the rotation of the tank itself is taken into account. (here the interpolation is removed).

gun.localRotation = Quaternion.AngleAxis(_cacheCamera.eulerAngles.y - transform.eulerAngles.y, Vector3.up)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question