Vector maths help - Rotate object toward other on 1 axis

Take these two objects:

I want to rotate the plane so that its Y direction is facing the cube (as closely as possible), but the plane may only rotate around its local X axis. How to calculate this?

Thank you.

the version of rotate at the bottom of the scripting reference page for transform.rotate: Unity - Scripting API: Transform.Rotate

you can get the angle by using Vector3.angle (Unity - Scripting API: Vector3.Angle)
between the transform.forward and the vector between the two positions (a.position - b.position)

To look at the object use:

public Transform target;

void Update()

{
transform.LookAt(target);
}

I would sudjest you to rotate the object using Space.World.

transform.Rotate (Vector3.up * Time.deltaTime, Space.World);

That doesn’t make any sense, it’s impossible to do what you want if this is your condition

try, plane.up = box.position - plane.position.

Can you elaborate? I tried fetching the angle from the transform positions, and using it in rotate, but the plane would just keep spinning.

This will affect more than X axis.

It doesn’t need to face it 100%, just as closely as possible. They are using this technique in UE4 for light-beam effect. The rays are just a billboard basically with fixed position, but will rotate around one of its axis to face the camera.

https://docs.unrealengine.com/latest/INT/Resources/Showcases/BlueprintExamples/FogSheet/index.html

Sort of works but affects Z axis rotation too. I only want X axis to be used.

transform.rotate(new Vector3(Vector3.Angle(plane.up,(box.position-plane.position)),0,0);

Not sure if I’m doing something wrong but this is the result:

http://gfycat.com/LeftScaredIndianjackal

        transform.Rotate(new Vector3(Vector3.Angle(transform.up, (target.position - transform.position)), 0, 0));

Behavior added to Plane, target is cube transform.

Vector3 current = transform.localEulerAngles;
transform.localEulerAngles = new Vector3(Vector3.Angle(transform.up, (target.position - transform.position),current.y,current.z);

Doesn’t seem to do the trick:

http://gfycat.com/BaggyUnfoldedIchidna

        Vector3 current = transform.localEulerAngles;
        transform.localEulerAngles = new Vector3(Vector3.Angle(transform.up, (target.position - transform.position)), current.y, current.z);

Some thing like this?

Lookat ( this.x, target.y, target.z)

You’re doing it in the update function, do you need to keep it updated?

  1. calculate the angle between the object and where the object would be in the z plane of the current object.
  2. rotate that object on the x axis that angle in self space…
    Vector3 pos = transform.InverseTransformPoint (obj.position);
     pos.x = 0;
     float angle = Vector3.Angle (Vector3.up, pos);
     angle = pos.z < 0 ? -angle : angle;
     transform.Rotate (Vector3.right, angle, Space.Self);

This way, you ignore the difference in position from the object to the transform laterally. This allows you to rotate only in one axis and only in the space of the object.

1 Like

Yes, it needs to be kept updated.

Sort of works, but behaves strangely when the other object is moving:

http://gfycat.com/ComplicatedUnsteadyHorsemouse

Sorry, its pos.z, not pos.y.

I corrected it in my post.

1 Like

Works splendidly. Thanks!