2D project: Align a object's rotation to another object
Hello everyone! Excuse me for any grammatical errors. I found many solutions to my problem but I can't use these because are created for a 3d game. I'd like to align the rotation of an object to another object, a solution I found on the net is: public class Rotate : MonoBehaviour { // Drag and drop the MountPoint transform of the object holding the script public Transform child ;
// Drag and drop the MountPoint transform of the other object
public Transform target ;
private void Start()
{
Align();
}
private void Align()
{
Vector3 direction = target.position - child.position ;
// Change child.forward to child.up if you want the up vectors to "look at" the other child object
Quaternion rotation = Quaternion.FromToRotation(child.forward, direction);
transform.rotation = rotation * transform.rotation;
}
}
It works but the problem is that it edits the x and y components, doing that, the object just disappear, because my is a 2d game. I'd like to edit only the z component of the rotation, any ideas?
Your answer
Follow this Question
Related Questions
rotate in the direction of the child 1 Answer
Rotating on other axis 1 Answer
Align an object rotation in relation to another object 0 Answers
how do I rotate an object without affecting later rotation? 0 Answers
Simple Rotate Script won't work! 2 Answers