How do I get an object to Look At another object?


#1

I’m not very clear on how we get an object to look at an object or follow it.

I found this forum that talks about the logic but don’t know how to get that in code form.

My code looks as follows since I’m trying to make a node for this and tried following the logic of the nodes the OP has in their image in this post.

package armory.logicnode;

import armory.trait.physics.RigidBody;
import iron.object.Object;
import iron.math.Mat4;
import iron.math.Vec4;
import iron.math.Quat;

class LookAtNode extends LogicNode {

public function new(tree:LogicTree) {
	super(tree);
}

override function run() {
	var targeter:Object    = inputs[1].get();
    var target:Object      = inputs[2].get();
    var threeDeeTrack:Bool = inputs[3].get();

    var targetPos:Mat4 = target.transform.world;
    var lookPos:Vec4   = targetPos.look();
	var newRot = new Vec4();

	newRot.setFrom(lookPos);

    newRot.x *= 1.0;
    newRot.y *= 1.0;
    newRot.z *= 0.0;

    targeter.transform.rot.fromEuler(newRot.x, newRot.y, newRot.z);
    targeter.transform.buildMatrix();

    #if arm_physics
	var rigidBody = targeter.getTrait(RigidBody);
	if (rigidBody != null) rigidBody.syncTransform();
    #end

	super.run();
}

}


#2

interested in a solution for this. Thanks for the effort so far.


#3

Hey, glad you’re interested! I feel like i’m close to figuring it out but just not getting there yet.